HTML5 code not working in IE9

元气小坏坏 提交于 2019-12-25 08:10:03

问题


The following simple code works in Firefox (12.0) but does not seem to work in IE 9 even though local storage is supported in IE9. Notice how alert(localStorage.lastname); does not show up any results. Was wondering if there is a known issue in using localStorage in IE9 as the documentation does say it is supported.

   <!DOCTYPE html>
    <html>
    <body>
    <script> 
    if(typeof(Storage)!=="undefined")
    {
      alert('local storage')
      localStorage.lastname="Smith";
      alert(localStorage.lastname);  
    }
     else
    {
     alert("Sorry, your browser does not support web storage...")
    }
   </script>
   </body>
   </html>

回答1:


Never set/get the items in localstorage directly! Use the appropriate methods for that:

localStorage.setItem(key,value)
localStorage.getItem(key)
localStorage.removeItem(key)

This fixes your IE problem and you will live happily :-D

(Note, that the values are stores as strings!)




回答2:


Your code will work fine on IE if you host your HTML file on web server.

If you are opening file:// url in IE, then localStorage will be undefined.
Try confirming that with if(typeof(localStorage)!=="undefined") and you will get "Sorry, your browser does not support web storage..."




回答3:


actually it doesnt even work from a webserver. i am running a web app on websphere application server and i get the same issue on ie9. it works fine on chrome.

you can try the stuff on this page: http://html5doctor.com/storing-data-the-simple-html5-way-and-a-few-tricks-you-might-not-have-known/



来源:https://stackoverflow.com/questions/10916210/html5-code-not-working-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!