admin管理员组文章数量:1391975
I have a typical ajax-style dynamic content setup: click a link, change anchor in address bar, and load new content. When user clicks the back button, it cycles through all the anchors - as expected.
Is there a way (using JavaScript I'm assuming) to make the back button go to the previous page, and 'ignore' all the anchors?
I have a typical ajax-style dynamic content setup: click a link, change anchor in address bar, and load new content. When user clicks the back button, it cycles through all the anchors - as expected.
Is there a way (using JavaScript I'm assuming) to make the back button go to the previous page, and 'ignore' all the anchors?
Share Improve this question asked May 5, 2011 at 21:53 Eric Di BariEric Di Bari 3,8677 gold badges42 silver badges49 bronze badges3 Answers
Reset to default 5Count the stages the user pass and use
history.back(X);
or
history.go(-1*X);
in order to go back X pages/anchors.
First, to make sure Im understanding, you have named your AJAX bookmarks the same as IDs in your DOM elements? If so, anyway to undo this? You can trick it not to go down, but this is kludgy way of doing it. Instead you should have a ID like #somepage
and AJAX urls like #!/somepage
this way they don't get confused. Also, people like Google will not know normal #id
s are hash URLs, but doing #!/
gives Google a clue.
Now, if you want to do it using a #!/
method you need a timer for older browsers (<=IE7) But for "modern" browsers IE8, FF, and Chrome you can use the onhashchange JS property like:
window.onhashchange = function(){ console.log('hash has changed') };
Then, if you want to support older browsers you need to do something a little more advanced. You need to do:
var currentPage = '';
setTimeout(function(){
if(currentPage !== window.location.hash){
console.log('hash has changed');
currentPage = window.location.hash
}
},500);
In both examples you need to replace the console.log()
s with a function of your own that triggers the "change page" event.
To stop the scrolling you could add:
window.onhashchange = function(){
if (window.location.hash == "" || window.location.hash == "#"){ return false; }
//rest of your code here!
};
The return false will prevent the scrolling. This should work in the timer as well.
More Information:
https://developer.mozilla/en/DOM/window.onhashchange
http://msdn.microsoft./en-us/library/cc288209(VS.85).aspx
http://ajaxpatterns/Unique_URLs
location.replace(document.referrer)
this works for me. depending on your purpose, you can use it in the "onclick" or add some additional conditions via JavaScript e.g. if the referrer is empty See: In what cases will HTTP_REFERER be empty
本文标签: javascriptBack buttonskip anchorsStack Overflow
版权声明:本文标题:javascript - Back button - skip anchors - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744723193a2621817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论