Skip to content Skip to sidebar Skip to footer

Detecting Browser Back Button In Javascript/jquery Without Using Any External Plugins

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,FireFox,Chrome I googled so many lin

Solution 1:

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


Solution 2:

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!


Post a Comment for "Detecting Browser Back Button In Javascript/jquery Without Using Any External Plugins"