1

i have 3 divs , i want that on first time the page is loaded the function init runs,not again after tht on refresh or reload of page. but its not working below is my code, it runs the code even on refresh or reload of the page.

function _(x){
    return document.getElementById(x);
}

 $(document).ready(function(){       
            init();        
    });

function init() {   
        _("page1_id").style.display = "none";   
        _("page2_id").style.display = "none";       
        _("page0_id").style.display = "block";      
}

i am a newbie so kindly share the code tht i need to add or change..thank u

  • 3
    because reloading reruns all the code. Only way to not load it again is to use cookie or localstorage and set that it runs. – epascarello May 16 '18 at 16:18
  • Use localstorage to save state between page loads https://stackoverflow.com/questions/29986657/persist-variables-between-page-loads – zavg May 16 '18 at 16:19
  • You can use sessionStorage in javascript to manage these kind of condition. Go through https://www.w3schools.com/jsref/prop_win_sessionstorage.asp – Naveen Chandra Tiwari May 16 '18 at 16:19

1 Answers1

0

Set a cookie init_run = true then check it before running init() or inside the init function:

function init (){
 if ( cookie is set )
    return;
 ...
}
SteepZero
  • 334
  • 2
  • 8