简体   繁体   中英

Handling browser back button in javascript

I need to handle the browser back button in javascript. I have implemented the tabs functionality in JS. So I want the user to return to the exact time from where they navigated to another page.

I can keep the tab # in some hidden input field. and check with the same in javascript.

But how do I make sure that a function gets called in javascript when the user hits the back button. some help would be appreciated.

This plugin (from jQuery tools) does exactly what you want.

HTML:

<!-- tabs title --> 
<ul id="flowtabs"> 
    <li><a id="t1" href="#player_tab">The Player</a></li> 
    <li><a id="t2" href="#plugins_tab">Plugins</a></li> 
    <li><a id="t3" href="#streaming_tab">Streaming</a></li> 
</ul> 

<!-- tabs content --> 
<div id="flowpanes"> 
    <div>tab1 html here</div> 
    <div>tab2 html here</div> 
    <div>tab3 html here</div> 
</div>

Javascript:

<script src="http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js"></script> 
<script> 
$(function() {
    $("#flowtabs").tabs("#flowpanes > div", { history: true });
});
</script>

how about history.go(-1) ?

$(function(){ 

$("#myLink").click(function(){ 
history.go(-1);
});

});

I invite you to read this article .

Basically, it's based on the dhtmlhistory object, and it change the url in the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM