Browser back button handling

前端 未结 2 869
闹比i
闹比i 2020-11-29 07:28

I am trying to handle browser back button event but i could not find any solution.

I want to ask user if he clicks on browser back button using \"confirm box\" if h

相关标签:
2条回答
  • 2020-11-29 07:48

    Warn/confirm User if Back button is Pressed is as below.

    window.onbeforeunload = function() { return "Your work will be lost."; };
    

    You can get more information using below mentioned links.

    Disable Back Button in Browser using JavaScript

    I hope this will help to you.

    0 讨论(0)
  • 2020-11-29 07:58

    You can also add hash when page is loading:

    location.hash = "noBack";
    

    Then just handle location hash change to add another hash:

    $(window).on('hashchange', function() {
        location.hash = "noBack";
    });
    

    That makes hash always present and back button tries to remove hash at first. Hash is then added again by "hashchange" handler - so page would never actually can be changed to previous one.

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