Detect first page load with jQuery?

Jim Evans picture Jim Evans · Feb 28, 2012 · Viewed 42.6k times · Source

I need to detect the first time a page loads in jQuery so that I can perform some actions only when the page loads the first time a user navigates to that page. Similar to server side code page.ispostbasck. I have tested $(document).ready and it fires every time the page loads so this will not provide what I need. I have also tried the jQuery Load function - it also fires every page load. So by page load an example is that I have an HTML input tag on the page of type button and it does not fire a postback (like an asp.net button) but it does reload the page and fires $(document).ready

Thanks

Answer

Sarfraz picture Sarfraz · Feb 28, 2012

You will have to use cookie to store first load information:

if (! $.cookie("cookieName")){
   // do your stuff

   // set cookie now
   $.cookie("cookieName", "firstSet", {"expires" : 7})
}

Note: Above example uses jQuery Cookie plugin.