Detecting browser button in javascript / jquery without using any external plugins

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

I will support the following browsers

IE8, IE9, Firefox, Chrome

I googled so many links and found below a piece of code from a code project

<body onbeforeunload="HandleBackFunctionality()"> function HandleBackFunctionality() { if(window.event) { if(window.event.clientX < 40 && window.event.clientY < 0) { alert("Browser back button is clicked..."); } else { alert("Browser refresh button is clicked..."); } } else { if(event.currentTarget.performance.navigation.type == 1) { alert("Browser refresh button is clicked..."); } if(event.currentTarget.performance.navigation.type == 2) { alert("Browser back button is clicked..."); } } } 

The above code works with IE browser. this means that I can get the window.event.clientX and customerY values ​​in IE browser. but in chrome / firefox does not work.

When I click the "Back back" button, I need to refresh the page.

how can I detect a mouse click event in a browser in all browsers (IE, firefox, chrome)

Any help would be appreciated.

+5
source share
2 answers

You can simply drop it on your web page, and when the user clicks on it, he will call the function. The default function for this call is the javascript "Back Button Clicked" warning.

To replace this functionality, you just need to override the OnBack function. This can be done using the following code.

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

This will now replace the β€œBack Button Clicked” warning with β€œYou clicked on it!”. Alerts
Support for the following browsers IE8, IE9, FireFox, Chrome
Browser Button Detection
Or using jquery
Browser capture
Using standard javascript
Mastering the Back Button with Javascript

+4
source

Since you support IE 8 and 9, there is no single method that can help you solve this problem. You can use the html5 history API, but this does not support IE8 and 9. As mentioned in the above comment, you can use window.onPopState for chrome, firefox, etc. And for IE you can create a separate js file and use conditional statements to start it when the user uses IE.

For instance:

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

Learn more about the HTML5 Story API: onPopState by mozilla

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1202400/


All Articles