[英]Handle browser back button with Javascript / different approach possible by using anchors?
I'm currently struggling with a good navigation on a website using Ajax calls and unobstrusive JS.我目前正在努力使用 Ajax 调用和不显眼的 JS 在网站上进行良好的导航。 I'm catching the click event on links, load the content, attach it to a div and then return false.我正在捕捉链接上的点击事件,加载内容,将其附加到 div 然后返回 false。 This works quite well and also allows Google to crawl the site with speaky URLs.这非常有效,还允许 Google 使用语音 URL 抓取网站。
But I didn't know how to deal with the browser back button.但我不知道如何处理浏览器的后退按钮。 I found this solution to catch the event when the user clicks on the back button:当用户单击后退按钮时,我找到了这个解决方案来捕获事件:
http://www.bajb.net/2010/02/browser-back-button-detection/ http://www.bajb.net/2010/02/browser-back-button-detection/
It works quite well.它工作得很好。 But I also want the back button to work normally when the user found the website via a link and wants to return to the previous page (I don't want to trap anyone).但是当用户通过链接找到网站并想要返回上一页时,我也希望后退按钮正常工作(我不想困住任何人)。
When I thought about it the best way would be to use anchors.当我想到它时,最好的方法是使用锚。 The normal back button supports them and you can go back in history without reloading the page (/#1 <- /#2 <- /#3 etc.)正常的后退按钮支持它们,您可以 go 回到历史记录而无需重新加载页面(/#1 <- /#2 <- /#3 等)
It would work like this:它会像这样工作:
Only the last part isn't really clear for me and I could need help here.只有最后一部分对我来说不是很清楚,我在这里需要帮助。
Finaly, my question: Does this make any sense?最后,我的问题:这有意义吗?
Thanks!谢谢!
Just add the href
to window.location.hash
after loading the content into a div
.将内容加载到div
后,只需将href
添加到window.location.hash
。 Then you can use that back button detection script to load what ever is in the hash.然后,您可以使用该后退按钮检测脚本来加载 hash 中的内容。
I solved the problem by using this great jQuery Plugin: History.js我通过使用这个很棒的 jQuery 插件解决了这个问题: History.js
Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.