Detect if the user has used the back button

前端 未结 4 646
死守一世寂寞
死守一世寂寞 2021-01-04 20:08

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

相关标签:
4条回答
  • 2021-01-04 20:37

    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/

    0 讨论(0)
  • 2021-01-04 20:37

    Track when user hits back button on the browser

    There are multiple ways of doing it, though some will only work in certain browsers. One that I know off the top of my head is to embed a tiny near-invisible iframe on the page. When the user hits the back button the iframe is navigated back which you can detect and then update your page. Here is another solution.

    You might also want to go view source on something like gmail and see how they do it.

    Here's a library for the sort of thing you're looking for by the way

    0 讨论(0)
  • 2021-01-04 20:52

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

    0 讨论(0)
  • 2021-01-04 20:52

    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.

    0 讨论(0)
提交回复
热议问题