javascript - Local storage not working -
i have javascript need run once local storage save state.the script changes login in logout. when page refresh don't want script executed :
var str=document.getelementbyid("tst").innerhtml; var n=str.replace("login","logout"); document.getelementbyid("tst").innerhtml=n;
html :
<a href="login" title="log in" class="login_button" id="tst">login</a></span>
i tried code doesn't trigger script @ :
var eventsfired = localstorage.getitem('fired'); if (eventsfired != '1'){ if (document.getelementbyid('tst')){ var str=document.getelementbyid("tst").innerhtml; var n=str.replace("login","logout"); document.getelementbyid("tst").innerhtml=n; localstorage.setitem('fired', '1'); } }
js fiddle link http://jsfiddle.net/9t4wb/4/
i tried cookies using jquery.cookies.js , same result :
var eventsfired = ($.cookie('eventsfired') != null) ? $.cookie('eventsfired') : 0; if (eventsfired == 0){ if (document.getelementbyid('tst')) { var str=document.getelementbyid("tst").innerhtml; var n=str.replace("login","logout"); document.getelementbyid("tst").innerhtml=n; eventsfired++; $.cookie('eventsfired', eventsfired); }}
cookies jsfiddle test http://jsfiddle.net/jqxvj/5/
if need support more chrome users, can use amplifyjs
it's let use 1 syntax on browsers...
//set value amplify.store("mykey", myobject) //get value amplify.store("mykey") // return myobject
you can check the documentation , see advantages of plugin
Comments
Post a Comment