views:

335

answers:

4

My webpage runs a javascript function when the page is loaded. However, I don't want the function to run if the user comes back to this page using the back button. How can I prevent this using javascript?

$(document).ready(function(){
  // Do not run this function if the user has arrived here using the back button  
  RefreshThePage();
});
A: 

I think studying the way Struts handles duplicate form submissions could help you.

Some links:

http://www.techfaq360.com/tutorial/multiclick.jsp

http://www.java-samples.com/showtutorial.php?tutorialid=582

http://www.xinotes.org/notes/note/369/

thelost
A: 

The event object offers you to get the key code. so basically you register an eventlistener onKeyDown. Use the received event. if the keycode matches the key you like continue with your function.

document getElementById('elementId').onKeyDown = checkKey();
function checkKey(event) {
  if (event.keyCode === keyCode) then . . . 
}

play around with alert(event.keyCode); to find the right one.

Markus Maria Miedaner
Tom is referring to the browser's Back button, not a back button in the page. And I don't think you can capture the browser's back button as a keyboard event in JavaScript.
Prutswonder
+3  A: 

I'd have thought that using cookies is the easiest way to do this

matpol