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'?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With