3

I have to capture if user clicks on back button of browser(IE, Chrome and Firefox)

I don't want onbeforeunload event to be called when user presses the back button of browser

user2078643
  • 95
  • 1
  • 2
  • 12
  • 1
    possible duplicate of [detect back button click in browser](http://stackoverflow.com/questions/6359327/detect-back-button-click-in-browser) – MaVRoSCy Apr 29 '15 at 08:01
  • 1
    There's already a question about this with an answer on SO: http://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser – lins Apr 29 '15 at 08:02
  • there are multiple answers for questions like this, please look at: http://stackoverflow.com/search?q=Javascript+back+button – Henrik Apr 29 '15 at 09:17
  • Could you suggest me any one from the link you suggested as i am unable to fine the answer of my question. – user2078643 Apr 29 '15 at 09:38
  • 1
    possible duplicate of [Intercepting call to the back button in my AJAX application: I don't want it to do anything!](http://stackoverflow.com/questions/1844491/intercepting-call-to-the-back-button-in-my-ajax-application-i-dont-want-it-to) – Alias Varghese Aug 26 '15 at 05:32

2 Answers2

3

Update In the following function when u click on links you will see the event being fired....now after clicking on any link click back button youll agin c the event being fired.

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>

Use the hashchange event:

window.addEventListener("hashchange", function(e) {
  // ...
})
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
1

I was looking for the same thing a while ago, but the Back button event of the browsers cannot be listened to. But, if it could be handy, you can use jQuery (or Javascript) and use the .unload() method

Jquery .unload() API

Andrea Giachetto
  • 177
  • 1
  • 1
  • 8