每次加载页面后运行 JavaScript
我有一个简单的问题,但它困扰了我很多天,而且我找不到解决方案。 我想在每次加载或呈现页面时触发 JavaScript 事件。
有任何机构可以帮忙吗?
I have a simple question but its haunting me since many days and I couldn't find the solution.
I would like to fire a JavaScript event for every time the page is loaded or rendered.
Can any body please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
查看此线程上的一些缺点和解决方法:点击后退按钮时是否有跨浏览器onload事件?
[编辑]或更好(?),使用
:(从此帖子)
you can use
<BODY onLoad="alert('hello world!')">
See some drawbacks and workaround on this thread: Is there a cross-browser onload event when clicking the back button?
[EDIT] or better (?), use:
(from this thread)
尝试使用这个:
虽然最好的方法可能是使用 jQuery 的
ready()
功能确保浏览器兼容性。Try using this:
Although the best way would probably be to use jQuery's
ready()
function to ensure browser compatibility.如果您的要求是脚本需要在页面完全加载后执行,您可以按照以下方式编写
document.ready 在加载页面 html 元素时工作...上面的代码在整个页面(及其样式和图像等)因此我认为这需要单独提及
if your reqirement is such that the script needs to execute after the page has loaded completely you could write it in the following way
document.ready works when the page html elements are loaded... the above code executes after the entire page (with its styling and images etc.) hence i believe that this requires a seperate mention