Does IE8 out-of-the-box have support for 'localStorage'

JavascriptHtmlInternet ExplorerInternet Explorer-8

Javascript Problem Overview


I am trying to use the HTML5 feature localStorage. According to this blog it can be done using IE8, however when I try to use it I get a javascript error 'localStorage is null or not an object'

So my question: can localStorage be used by IE8 out-of-the-box? Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>IE8 - DOM Storage</title>
    <script type="text/javascript"> 
        function Save() {
	        localStorage.setItem('key','value');            
        }        
    </script>
</head>
<body>        
    <button onclick="Save();">
        Save
    </button>  
</body>
</html>

Javascript Solutions


Solution 1 - Javascript

It does support localStorage, though you need to be in IE8 mode (this will not work in IE7 mode).

To check that you're working in IE8 mode, load up the developer console. At the top, make sure that IE8 mode is selected. Standards mode would also be nice.

One thing that you also want to make sure of is that you're using the HTML5 doctype. You shouldn't be able to use an XHTML doctype with HTML5 features.

<!DOCTYPE html>

Using this doctype should not impact your browser support.

Also, make sure you access window.localStorage. It shouldn't be an issue, but IE has been known to host weirder issues. Perhaps it's looking for a locally scoped localStorage object? Who knows.

Solution 2 - Javascript

the comment of musicfreak was correct. Because this feature requires domain, I had to use it only through a live url (at least localhost) and not by opening it as a file from a disk.

There is no need to add window.localStorage as IE8 recognizes just localStorage as well.

Solution 3 - Javascript

Expanding upon a previous good Answer:

I had this same problem --worse! even IE10 failed!-- and I discovered the solution (for me) had more to do with my development environment than Internet Explorer. What I had done was create an HTML file, and edit it a lot using a good text editor. I could click-drag the file into a browser to see my progress in developing the page, and later simply refresh the browser window, when I updated/edited various things in the file.

It turns out that for Chrome and Firefox and Opera, window.localStorage was a valid object, but for Internet Explorer, it was "undefined" (as seen in the debugger). However, once I fired up a Web Server program (for "localhost"), and used that to feed the HTML page to the browser, then Internet Explorer provided a valid object ("DispHTMLStorage") for window.localStorage. Also note that in Tools/Options/Advanced for IE, there is a checkbox "enable DOM storage" --it appears to be checked by default, but it is always possible that some user will have manually disabled it.

Solution 4 - Javascript

Try using "IE=edge" instead of "IE=8" in your x-ua-compatible meta tag
From Microsoft: (link)

> Use the following value to display the webpage in EdgeHTML mode, which is the highest standards mode supported by Internet Explorer, from Internet Explorer 6 through IE11.

<meta http-equiv="x-ua-compatible" content="IE=edge" >

Solution 5 - Javascript

Local storage concept should work on IE8+.

All you have to do is put your source code on the server and run it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpidermanView Question on Stackoverflow
Solution 1 - JavascriptmattbastaView Answer on Stackoverflow
Solution 2 - JavascriptSpidermanView Answer on Stackoverflow
Solution 3 - Javascriptvernonner3voltazimView Answer on Stackoverflow
Solution 4 - JavascriptfoxontherockView Answer on Stackoverflow
Solution 5 - JavascriptVenkataramana Naik PalthyaView Answer on Stackoverflow