JavaScript localStorage object broken in IE11 on Windows 7

JavascriptHtmlWindows 7Local StorageInternet Explorer-11

Javascript Problem Overview


The localStorage object in Internet Explorer 11 (Windows 7 build) contains string representations of certain functions instead of native calls as you would expect.

This only breaks with vanilla JavaScript and sites like JSFiddle have no problem with this code but I suspect it's because there are localStorage polyfills in place that correct it.

Take this HTML page code for example:

<!DOCTYPE html>
<script>
  localStorage.setItem('test', '12345');
  alert(localStorage.getItem('test'));
  localStorage.clear();
</script>

This works perfectly well in all my installed browsers except for IE11. An error occurs on the first line 'SCRIPT5002: Function expected'.

Taking a look at what type the setItem function actually is in the IE developer tools console, states that it's a string...?

    typeof localStorage.setItem === 'string' // true

Printing out the string for setItem displays the following:

"function() {
var result;
callBeforeHooks(hookSite, this, arguments);
try {
result = func.apply(this, arguments);
} catch (e) {
callExceptHooks(hookSite, this, arguments, e);
throw e;
} finally {
callAfterHooks(hookSite, this, arguments, result);
}
return result;
}"

Oddly enough, not all functions have been replaced with strings, for example, the corresponding getItem function is indeed a function and works as expected.

    typeof localStorage.getItem === 'function' // true

Changing the document mode (emulation) to 10 or 9 still doesn't resolve the problem and both result in the same error. Changing the document mode to 8 gives the following error 'Object doesn't support this property or method' which is expected since IE8 doesn't support localStorage.

Is anyone else having the same issue with IE11 on Windows 7 where the localStorage object seems 'broken/corrupt'?

Javascript Solutions


Solution 1 - Javascript

Turns out this is a problem in the base version of IE11 (11.0.9600.16428) for Windows 7 SP1.

After installing a patch to update to 11.0.9600.16476 (update version 11.0.2 - KB2898785) the issue gets resolved. Links to other versions of Windows (32-bit etc.) can be found at the bottom of the patch download page.

Solution 2 - Javascript

It's not just IE11's fault.

Probably WEINRE is injected into the page. It hooks into several system functions to provide Developer Tools functionality, but IE11 interprets assignments to the localStorage and sessionStorage properties wrong, and converts the hook functions into strings, as if they were the data that is going to be stored.

There's a comment in the apache/cordova-weinre repo which says:

        #In IE we should not override standard storage functions because IE does it incorrectly - all values that set as
        # storage properties (e.g. localStorage.setItem = function()[...]) are cast to String.
        # That leads to "Function expected" exception when any of overridden function is called.
        object[property] = hookedFunction  unless navigator.userAgent.match(/MSIE/i) and (object is localStorage or object is sessionStorage)

Looks like it's either an old version of WEINRE being used, or this change hasn't been officially released (it's been there since 2013).

Solution 3 - Javascript

My localStorage returned undefined and I couldn't figure out why - until I realized that it was because I was running the HTML-page (with the localStorage script) directly from my computer (file:///C:/Users/...). When I accessed the page from a server/localhost instead it localStorage was indeed defined and worked.

Solution 4 - Javascript

In addition to the already excellent answers here, I'd like to add another observation. In my case, the NTFS permissions on the Windows %LOCALAPPDATA% directory structure were somehow broken.

To diagnose this issue. I created a new Windows account (profile), which worked fine with the localStorage,so then I painstakingly traversed the respective %LOCALAPPDATA%\Microsoft\Internet Explorer trees looking for discrepancies.

I found this gem:

C:\Users\User\AppData\Local\Microsoft>icacls "Internet Explorer"
Internet Explorer Everyone:(F)

I have NO idea how the permissions were set wide open!

Worse, all of the subdirectories has all permissions off. No wonder the DOMStore was inaccessible!

The working permissions from the other account were:

 NT AUTHORITY\SYSTEM:(OI)(CI)(F)
 BUILTIN\Administrators:(OI)(CI)(F)
 my-pc\test:(OI)(CI)(F)

Which matched the permissions of the parent directory.

So, in a fit of laziness, I fixed the problem by having all directories "Internet Explorer" and under inherit the permissions. The RIGHT thing to do would be to manually apply each permission and not rely on the inherit function. But one thing to check is the NTFS permissions of %LOCALAPPDATA%\Microsoft\Internet Explorer if you experience this issue. If DOMStore has broken permissions, all attempts to access localStorage will be met with Access Denied.

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
QuestionBrutalDevView Question on Stackoverflow
Solution 1 - JavascriptBrutalDevView Answer on Stackoverflow
Solution 2 - JavascriptsompylasarView Answer on Stackoverflow
Solution 3 - Javascriptholm50View Answer on Stackoverflow
Solution 4 - JavascriptPhillip RemakerView Answer on Stackoverflow