window.onload 已经触发了吗?
我有一个脚本将在未知时间插入未知页面。是否可以从该脚本判断 window.onload 是否已触发?
我无法控制主页。我无法向其中添加任何标记或脚本。我所知道的是我的脚本将在某个时刻插入到页面中。它可以在 window.onload 之前或之后;我需要检测我站在该事件的哪一边。
I have a script that will be inserted into an unknown page at an unknown time. Is it possible to tell from this script if window.onload has fired yet?
I have no control over the host page. I can't add any markup or scripts to it. All I know is that my script will be inserted into the page at some point. It could be before window.onload or after; and I need to detect which side of that event I'm on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新答案:
看看这个网站。他使用了一个技巧,看看最后一个元素是否document.getElementsByTagName("*") 是否已定义。它似乎可以在 Opera 和 IE 中运行。
原始答案:
你无法检查,但你可以这样做:
这肯定会在页面加载后完成你的工作。
Updated Answer:
Look at this site. He uses a trick by seeing if the last element of document.getElementsByTagName("*") is defined or not. It seems to work in Opera and IE.
Original Answer:
You can't check, but you can do:
That will do your stuff definitely after the page has loaded.
jQuery 的
ready()
将在文档加载时将要触发的函数排队,或者在文档已加载时立即触发:只需确保 jQuery 包含在脚本块之前即可。如果您无法直接包含任何脚本资源,则可以随时复制 jQuery-min 在你的脚本本身。
jQuery's
ready()
will queue a function to be fired when the document is loaded, or fire immediately if the document is already loaded:Just be sure jQuery is included before your script block. If you can't include any script resources directly, you can always copy the body of jQuery-min in your script itself.