window.onload 已经触发了吗?

发布于 2024-08-07 06:15:45 字数 154 浏览 6 评论 0原文

我有一个脚本将在未知时间插入未知页面。是否可以从该脚本判断 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 技术交流群。

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

发布评论

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

评论(2

酒几许 2024-08-14 06:15:45

更新答案:
看看这个网站。他使用了一个技巧,看看最后一个元素是否document.getElementsByTagName("*") 是否已定义。它似乎可以在 Opera 和 IE 中运行。


原始答案:
你无法检查,但你可以这样做:

window.setTimeout(function () {
    // do your stuff
}, 0);

这肯定会在页面加载后完成你的工作。

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:

window.setTimeout(function () {
    // do your stuff
}, 0);

That will do your stuff definitely after the page has loaded.

冷清清 2024-08-14 06:15:45

jQuery 的 ready() 将在文档加载时将要触发的函数排队,或者在文档已加载时立即触发:

$(document).ready(function() {
    //your code here
});

只需确保 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:

$(document).ready(function() {
    //your code here
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文