有没有办法在页面上后退或前进时触发任意 JavaScript?
具体来说,我希望通过 AJAX 模仿后退按钮行为,并且我不想将 #uglyhashes 附加到每个 URL,这似乎是我见过的大多数解决方案的标准做法。
(就其价值而言,我完全可以接受在 IE 中根本不起作用的解决方案。:P)
Specifically, I'm looking to mimic back button behavior with AJAX, and I don't want to attach #uglyhashes to every URL, which seems to be the standard practice of most solutions I've seen.
(For what it's worth, I'm perfectly fine with a solution that does not work at all in IE. :P )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将侦听器附加到文档的“DOMContentLoaded”事件,以在页面加载时触发 JavaScript。 这应该在后退和前进以及页面最初加载时触发。 如何使用 JavaScript 来实现您想要的行为取决于您——大多数人处理的方式是使用“#uglyhashes”。
编辑添加 Mozilla 示例代码(并更新事件名称):
You can attach a listener to the document's 'DOMContentLoaded' event to fire javascript when the page loads. This should fire when going back and forward as well as when the page initially loads. How you use that javascript to implement the behavior you want is up to you -- the way most people handle is with the "#uglyhashes".
Edited to add example code for Mozilla (and to update event name):