is there any function that can tell if the page is being loaded for the first time? I need to call a function only when the page is initially loaded.

chris_upjohn
—
2011-10-30T04:22:25Z —
#2

You could use localStorage to determine whether a user has visited or not but it only works in modern browsers and IE8+, for a fallback if you need to support IE7 and below cookies would be the next option.

skelleex
—
2011-10-30T04:39:45Z —
#3

SgtLegend said:

You could use localStorage to determine whether a user has visited or not but it only works in modern browsers and IE8+, for a fallback if you need to support IE7 and below cookies would be the next option.

ok i don't need the function to call the first time they ever visited the site, but each time they visit the site. something like a

window.onload

felgall
—
2011-10-30T05:26:21Z —
#4

If you place your JavaScript at the bottom of the page where it belongs then it can run straight away without needing window.onload

skelleex
—
2011-10-30T19:47:48Z —
#5

felgall said:

If you place your JavaScript at the bottom of the page where it belongs then it can run straight away without needing window.onload

ok understood but how would I know if the function is being called for the first time. Ok the senario is that the first function my page loads is function1. However the first time function1 is called it needs to do something like replaceState. However the next time I call function1 while on this site it checks to see if it was called before if not then no replaceState. I hope this better explains what I am trying to do.

felgall
—
2011-10-30T20:14:55Z —
#6

function1 = function() {
// code to run only the first time function1 is called goes here
function1 = function() {
// code to run every time function1 is called goes here
}
function1(); // leave out this line if the code to run every time should not run the first time
}

skelleex
—
2011-11-02T17:49:38Z —
#7

felgall said:

function1 = function() {
// code to run only the first time function1 is called goes here
function1 = function() {
// code to run every time function1 is called goes here
}
function1(); // leave out this line if the code to run every time should not run the first time
}