javascript - Setting and checking localstorage for a certain value, then performing a function based on the data -
i'm trying set local storage 1 page("index.html/settest.html"), , check on "index.html". if check comes result, it'll execute function.
i have written code this, doesn't work. don't know why, i'm hoping assistance here.
here's have on "settest.html" page. it's simple -
<script> window.onload=setlocalstorage() { localstorage.setitem("one", true); } </script>
so way understand it, when page loads, should set value of "one" true in localstorage.
here's have on "index.html" page -
<script> window.onload=setinterval(function() { var 1 = localstorage.getitem('one') || ''; if (one != 'yes') { function hideone() { var elem = document.getelementbyid("one"); elem.classname = "hide"; } } }, 1000); </script>
from understand, should check localstorage every second "one", , execute function "hideone" if comes yes(or true).
however, when go "settest.html", , visit "index.html", nothing happens. there no errors in console, or abnormal showing. don't why won't work.
thanks in advance, if needs more information or context feel free ask!
-mitchyl
you're not defining window.onload
functions correctly. either:
<script> window.onload = function() { localstorage.setitem("one", true); } </script>
or:
<script> window.onload = loadfunction; function loadfunction() { localstorage.setitem("one",true); } </script>
and on other page:
window.onload = function() { setinterval(function() { var 1 = localstorage.getitem('one') || ''; if (one != 'yes') { function hideone() { var elem = document.getelementbyid("one"); elem.classname = "hide"; } } }, 1000); };
additionally, you're setting localstorage.one
true
on first page, , checking if it's yes
on other page. not sure if meant or mistake.
Comments
Post a Comment