How to check if a page is loaded for the first time using javascript

I want to check if the page loads for the first time, and if it displays a filter. If I put showFiltermenu () in the pageLoad function, it will be displayed every time the page loads, but I just want it to be displayed for the first time. I tried using Page.IsPostBack , but this does not display a filter.

  <script type="text/javascript"> function showFiltermenu() { $("#filtermenuDrop").toggle('fold', {}, 500); } function closefiltermenu() { $("#filtermenuDrop").toggle('fold', {}, 500); } function pageLoad() { $("input[rel^='datepicker']").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, yearRange: "c-50:c+50", changeYear: true, showOn: "both", firstDay: 1, buttonImage: "../images/icons/buttons/basic1-049-small.png" }); <% if (Page.IsPostBack) { %> showFiltermenu(); <% } %> ShadowboxInit(); } 
+5
source share
3 answers

Thanks to everyone for their answers, but there was just a mistake in my question. I forgot ! in the if statement. Now it works to check if the page is loaded the first time.

 <% if (!Page.IsPostBack) { %> showFiltermenu(); <% } %> 
+1
source

with localStorage you can save these values:

 var firstTime = localStorage.getItem("first_time"); if(!firstTime) { // first time loaded! localStorage.setItem("first_time","1"); } 

no jQuery, no plugins, just clean, nice and fast HTML5 API

+7
source

Try with cookie. Cookies are very useful for this kind of thing. First of all, let's see if the browser knows the cookie:

 if(document.cookie) {document.cookie="Name=Value"; } else{ alert("perhaps you must change browser.Something in this page can't load.") } 

Remember that cookie always returns a string if you write to the console

  document.cookie 

Setting a cookie is easy. You can set many cookies. But if you delete the cookies, browser, then what you visit on the site for the first time. To check if a cookie is set, you can do this:

  if(document.cookie.search="value") {//code that you want to execute if the page is visited yet} 
+1
source

All Articles