detecting browser back button in javascript/jquery without using any external plugins

前端 未结 2 1832
别那么骄傲
别那么骄傲 2021-01-03 01:08

I want to detect browser back button in java-script/jquery without using any external plugins.

I am going to support following browsers

IE8,IE9,FireF

相关标签:
2条回答
  • 2021-01-03 01:33

    That can be simply dropped into your web page, and when the user clicks back, it will call a function. The default function on this call is a javascript alert “Back Button Clicked”.

    To replace this functionality, you simply need to override the OnBack function. This can be done by using the code below.

    <script type="text/javascript">
    bajb_backdetect.OnBack = function()
    {
       alert('You clicked it!');
    }
    </script>
    

    This will now replace the “Back Button Clicked” alert with a “You clicked it!’” alert.
    support following browsers IE8,IE9,FireFox,Chrome
    Browser Back Button Detection
    Or using jquery
    Catching browser back
    Using standard javascript
    Mastering The Back Button With Javascript

    0 讨论(0)
  • 2021-01-03 01:41

    Since you are supporting IE 8 and 9 as well, there isnt a single method that will help you solve this. You could use the history API of html5 but that doesn't support IE8 and 9. As mysteryos mentioned in the above comment, you could use window.onPopState for chrome, firefox etc, and for IE you could create a separate js file and use conditional statements to fire it when the user uses IE.

    For example:

    <!--[if lte IE 9]>
        <script src="youIESpecificfile.js"></script>
    <![endif]-->
    

    Read more about the HTML5 History API here: onPopState by mozilla

    Hope this helps!

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