Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript localStorage object broken in IE11 on Windows 7

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'?

like image 832
BrutalDev Avatar asked Jan 16 '14 06:01

BrutalDev


People also ask

Does localStorage work in IE?

The localStorage object stores the data like a persistent cookie, with no expiration date. The data will not be deleted when the browser is closed, and will be available when a user returns to the browser. IE also supports localStorage from IE8 but it does not support localStorage in IE7 and previous versions.

How do I access localStorage in JavaScript?

There are four basic JavaScript methods you can use to access and work with localStorage: setItem() - takes a key-value pair and adds it to localStorage. getItem() - takes a key and returns the corresponding value. removeItem() - takes a key and removes the corresponding key-value pair.

Can we store JavaScript object in localStorage?

In summary, we can store JavaScript objects in localStorage by first converting them to strings with the JSON. stringify method, then back to objects with the JSON. parse method.


1 Answers

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.

like image 158
BrutalDev Avatar answered Sep 29 '22 04:09

BrutalDev