2014-05-02 13 views
0

我使用下面的代碼保存在webstorage對象幾個內容,但我得到這個錯誤:對象不支持屬性或方法「setItem」

Error:-- Object doesn't support property or method 'setItem'

什麼建議嗎?我在IE(11)和Chrome(版本34.0.1847.131米 - 最新版本)都試過,同樣的問題。

<!DOCTYPE html> 
<html>  
    <body>   
     <div id="result">Test</div>    
     <script> 
      // Check browser support 
      alert(typeof(Storage)) 
      if (typeof(Storage) != "undefined") 
      { 
       // Store 
       Storage.setItem("lastname", "Smith"); 
       // Retrieve 
       document.getElementById("result").innerHTML=localStorage.getItem("lastname"); 
      } 
      else 
      { 
       document.getElementById("result").innerHTML="Sorry, your browser does not support Web Storage..."; 
      } 
     </script>   
    </body>  
</html> 
+0

使用localStorage而不是Storage。 – Ibrahim

回答

1

StoragelocalStorage有型動物。

From MDN,這裏有Storage一條簡短定義:

Storage is a SQLite database API. It is available to trusted callers, meaning extensions and Firefox components only.

所以,你的情況,你應該使用localStorage您使用Storage各一次。

請注意,getter和setter函數不是必需的,您可以使用localStorage作爲對象。例如,所有這些行產生相同的結果:

localStorage.foo = "bar"; 
localStorage["foo"] = "bar"; 
localStorage.setItem("foo","bar"); 
+0

這適用於Chrome,Firefox。但不適合IE?如何做到這一點它:( – vineetv2821993

+0

@ vineetv2821993 http://caniuse.com/namevalue-storage - 搜索一個polyfill! – Bigood

相關問題