1

I have made some html/JavaScript codes like this:

<script>
if(window.localStorage!==undefined)
{
 }
else{ alert('Your browser is outdated!'); }
function myfunction1{
if(localStorage.getItem('permission')!="") { localStorage.setItem('permission', pressed);} 
function myfunction2{ document.getElementById('data').innerHTML = localStorage.getItem('permission');}
</script>
<body onload="myfunction2()">
<p id="data">this paragraph shows storage data</p>
<button onclick="myfunction1()">press</button>

The first script is for checking whether the browser supports localstorage. Second script (myfunction1) checks if there is any data called "permission". If there is none, then it will make one. Third code (myfunction2) is for showing the localstorage data. But none of these are working. Maybe there is a simple problem on my code. But I can't fix it. Please help.

cyborg86pl
  • 2,597
  • 2
  • 26
  • 43
user8575948
  • 23
  • 1
  • 7

1 Answers1

1

You have several syntax errors in your code:

  1. You can check wether you web browser supports localStorage or not, just by doing typeof(Storage) === "undefined", works for localStorage / sessionStorage.
  2. Your functions need the correct syntax i.e. function name() {handle}
  3. You didn't defined pressed, if a String is what you needed you forgot the double quotes"pressed" around it.

<script>
if(typeof(Storage) === "undefined") {
  alert('Your browser is outdated!'); 
}

function myfunction1() {
  if(localStorage.getItem('permission')!="") 
  { localStorage.setItem('permission', 'pressed');} 
}

function myfunction2()  { 
  document.getElementById('data').innerHTML = localStorage.getItem('permission');
}

</script>

<body onload="myfunction2()">
<p id="data">this paragraph shows storage data</p>
<button onclick="myfunction1()">press</button>
</body>
dvr
  • 885
  • 5
  • 20
  • Your code have what I need. But there is a mistake in your code. if( typeof(Storage)!== "undefined") { alert('Your browser is outdated!'); } : this code is for making an alert, if typeof(Storage) is not undefined. Or if typeof(Storage) is defined. Change it to if(typeof(Storage) == "undefined") { alert('Your browser is outdated!'); } – user8575948 Sep 08 '17 at 01:47
  • My browser is alerting "Your browser is outdated" every time, when I open the page(which has the code from your answer). But localstorage setitem and getitem are working perfectly. – user8575948 Sep 08 '17 at 01:49
  • I'll choose your answer as correct. But requesting you to edit the answer, so that it may be helpful for other visitors. – user8575948 Sep 08 '17 at 01:50
  • @user8575948 Thanks for pointing that out, just edited it. Glad I could help. – dvr Sep 08 '17 at 05:38
  • your answer helped me, a lot – user8575948 Sep 08 '17 at 05:39
  • does it need 3=? – user8575948 Sep 08 '17 at 05:40
  • As for your question about conditions: == will convert the object and compare it, where === will only compare without doing any further conversion. You can refer to this question: https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – dvr Sep 08 '17 at 05:53