有没有办法在页面上后退或前进时触发任意 JavaScript?

发布于 2024-07-08 08:42:21 字数 129 浏览 6 评论 0原文

具体来说,我希望通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2024-07-15 08:42:21

您可以将侦听器附加到文档的“DOMContentLoaded”事件,以在页面加载时触发 JavaScript。 这应该在后退和前进以及页面最初加载时触发。 如何使用 JavaScript 来实现您想要的行为取决于您——大多数人处理的方式是使用“#uglyhashes”。

编辑添加 Mozilla 示例代码(并更新事件名称):

document.addEventListener("DOMContentLoaded", function() {
   // Do watchya like
},false);

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):

document.addEventListener("DOMContentLoaded", function() {
   // Do watchya like
},false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文