10 Aralık 2015 Perşembe

JS : JavaScript LocalStorage and SessionStorage

The storage objects enable you to set data that is "remembered" as the user visits all of the pages on your domain. There are two types of storage objects that we can establish, sessionStorage andlocalStorage. sessionStorage data is only available in the tab it was established in, and will expire when the tab is closed. localStorage data is more long term, it is available in all tabs and remains persistent even if the browser software is closed then re-opened.
  1. //Page1.html    
  2. < script >  
  3.     // Establish both types of storage data for a test    
  4.     localStorage.setItem("firstname""Kaushik"); // persistent data    
  5. sessionStorage.setItem("lastname""S"); // persistent session data    
  6. < /script>   < a href = "page2.html" > Go to other page < /a>    

Notice how sessionStorage data is not available when you open Page2.html in a new tab, or if the browser is closed. And notice how localStorage data remains persistent even in new browsing tabs and when browser is closed/opened.  

  1. page2.html  
  2.   
  3.  < script > document.write(localStorage.firstname + " " + sessionStorage.lastname); < /script>    

Hiç yorum yok: