How to find whether the user clicks browser back button or Refresh button

前端 未结 8 1335
感动是毒
感动是毒 2021-02-07 12:59

I need to find whether the user clicking the browser back button or Refresh button.

I need to redirect the page to Error page when he clicks the back or refresh button.

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-07 13:45

    I presume you want to do this because of post-back to avoid resubmitting some action or data right? ironically, this wasn't as huge of a problem back before asp.net webforms ... because you generally had to post to a different URL instead of the same one (much like asp.net mvc actually).

    One trick I liked to use ... even though in the most technical of points is slower ... was to have an input page, and a post page. The HTML output of the post page was some super minimal HTML with a tiny javascript that replaced the current url (of the post page) in the browser's history with the result page (or even the original referring page if you really wanted). The result was when the user would click the back button, he'd be sent to the original input page, or if he clicked refresh, he'd already be on the new result page.

    
    

提交回复
热议问题