最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to capture back button event in Chrome,Firefox and IE - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Apr 29, 2015 at 9:12 user2078643 asked Apr 29, 2015 at 7:57 user2078643user2078643 952 gold badges2 silver badges12 bronze badges 5
  • 1 possible duplicate of detect back button click in browser – MaVRoSCy Commented Apr 29, 2015 at 8:01
  • 1 There's already a question about this with an answer on SO: stackoverflow./questions/25806608/… – lins Commented Apr 29, 2015 at 8:02
  • there are multiple answers for questions like this, please look at: stackoverflow./search?q=Javascript+back+button – Henrik Commented Apr 29, 2015 at 9:17
  • Could you suggest me any one from the link you suggested as i am unable to fine the answer of my question. – user2078643 Commented Apr 29, 2015 at 9:38
  • 1 possible duplicate of Intercepting call to the back button in my AJAX application: I don't want it to do anything! – Alias Varghese Commented Aug 26, 2015 at 5:32
Add a ment  | 

2 Answers 2

Reset to default 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./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://github./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) {
  // ...
})

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

发布评论

评论列表(0)

  1. 暂无评论