Detect if the user has used the back button

前端 未结 4 673
死守一世寂寞
死守一世寂寞 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: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.

提交回复
热议问题