Create my own storage API on top of localStorage so it is easier to use in my own code.
So it has an API for my purposes and can be extended later.
Chrome Version: Version 22.0.1229.94 m
JSFiddle: http://jsfiddle.net/ttback/kr88W/
The fiddle will trigger an error in Console:
This works in IE 8 and Firefox 16.0.1, so I am wondering if I'm missing something or is it a Chrome bug.
<html>
<head>
<title>JS function not working in Chrome</title>
<script type="text/javascript">
var localStorage = localStorage || {};
localStorage.localStorageSave = function(){
alert("SAVE");
};
localStorage.localStorageSave();
</script>
</head>
<body>
</body>
</html>
You can access the localStorage functionality through the Window. localStorage property. This property provides several methods like setItem(), getItem(), and removeItem(). You can use these to store, read, and delete key/value pairs.
localStorage browser support To be sure the browser supports localStorage , you can check using the following snippet: if (typeof(Storage) !== "undefined") { // Code for localStorage } else { // No web storage Support. }
When you do
localStorage.localStorageSave = function(){
alert("SAVE");
};
you're doing the same as
localStorage["localStorageSave"] = function(){
alert("SAVE");
};
or
localStorage.setItem("localStorageSave", (function(){
alert("SAVE");
}).toString());
You're in fact not saving a function in localStorage, because localStorage saves only strings.
You can check it with
console.log(typeof(localStorage.localStorageSave));
You get "string"
. This isn't a function and is normal.
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