How to capture button event in Chrome, Firefox and IE

I need to capture if the user clicks the back button of the browser (IE, Chrome and Firefox).

I do not want the onbeforeunload event to be called when the user clicks the browser back button

+3


source to share


2 answers


Update In the next function, when you click on the links, you will see that the event is fired .... now after clicking on any link, click the button where you will fire the event.

JSFiddle



$(window).bind( "hashchange", function(e) {
  alert()
  });
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://github.com/cowboy/jquery-bbq/raw/v1.2.1/jquery.ba-bbq.min.js"></script>

<nav id="siteNav">
<ul>
<li class="nav1"><a href="#landing" title="Home">Home</a>
<li class="nav2"><a href="#products" title="Products">Products</a>
<li class="nav3"><a href="#about-us" title="About Us">About Us</a>
<li class="nav4"><a href="#sign-up" title="Sign Up">Sign Up</a>
<li class="nav5"><a href="" title="Videos">Videos</a>
</ul>
</nav>
      

Run codeHide result


Use event hashchange

:

window.addEventListener("hashchange", function(e) {
  // ...
})

      

+3


source


I was looking for the same thing a while ago, but the browser's back button event cannot be listened to. But if it can be convenient, you can use jQuery (or Javascript) and use the .unload () method



JQuery.unload () API

+1


source







All Articles