How do I detect in Javascript whether a user is arriving at a page from a link or coming back to the page via the Browser's Back button?

dwoolley3
dwoolley3 used Ask the Experts™
on
How do I detect in Javascript whether a user is arriving at a page from a link or coming back to the page via the Browser's Back button?

My users and I are using I.E. 7.0.

When the web page loads, I have a javascript routine that is called at startup, and when I display the document.referrer in an alert, it gives me the same URL in both cases. When arriving at the page via the Browser's back button, the URL displayed is the page from which it originally came. How do I differentiate between the two so that I don't execute my clearForm routine upon a user going "BACK" to the form?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
this seems to detect how you arrived


<script>
function start(){
  if (document.f1.hide.value==''){
   document.f1.hide.value='flag'
   alert("first visit") }
  else
  {alert("back button")}
}
</script>
 
 
<body onload="start()">
<form name=f1>
  <input type=hidden name=hide>
</form>
</body>

Open in new window

Author

Commented:
This worked perfectly for me! Thank you so much. You will receive credit from me.

I am still curious, though, if there is an actual command or field that detects if a user is coming to the page for the first time or using a back button from a browser. Evidently the browser knows the difference since it will not automatically clear out values upon detecting a back button press. I'll keep the post open for the weekend for any possible answers to this.

Author

Commented:
This worked perfectly for me.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial