页面渲染后执行 Javascript IE6

发布于 2024-08-19 07:21:23 字数 710 浏览 2 评论 0原文

在 IE 6 中完全呈现页面之前,我在运行一些 JavaScript 时遇到了问题(也许其他版本也是如此,但现在只是测试 IE6。Firefox 似乎没问题)。我可以通过像这样调用 window.onload 上的 js 来解决这个问题:

window.onload = function(){doIt();}

但是,我担心的是我会覆盖 window.onload 中可能已经存在的任何其他内容>。该代码将用作库的一部分,因此我不能保证 window.onload 不会被其他人设置在其他地方。我宁愿将我的函数附加到 onload 事件中,如下所示:

window.onload += function(){doIt1();}
window.onload += function(){doIt2();}

但是当我这样做时,只会调用 doit2() 。有没有办法在页面完全呈现时注册事件处理程序?我的第二个想法是将我的代码放入循环检查中,以确保我的所有对象在运行之前都存在。但我担心这可能会锁定浏览器。

只是为了一些背景信息,我的代码隐藏/显示 iFrame。我知道我可以使用 iFrame 的 onload 属性,但我需要在调用代码之前完全加载所有 iFrame。

社区有什么想法吗?预先感谢您的输入。

I am having trouble with some JavaScript running before the page is completely rendered in IE 6 (maybe other versions too but just testing IE6 for now. Firefox seems to be OK). I can get around this by calling the js on window.onload like this:

window.onload = function(){doIt();}

However, my concern is the fact that I will overwrite anything else that may already be in window.onload. The code will be used as part of a library so I can not guarantee that window.onload will not be set somewhere else by someone else. I would rather append my function to the onload event like this:

window.onload += function(){doIt1();}
window.onload += function(){doIt2();}

But when I do so, only doit2() is called. Is there a way to register an event handler for when the page is fully rendered? My second thought would be to just put my code in a loop checking to make sure all my objects exist before running. But I am scared that this could potentially lockup the browser.

Just for some background info, my code is hiding/showing iFrames. I know that I can use the iFrame's onload attribute but I need all of the iFrames to be fully loaded before calling the code.

Any thoughts from the community? Thanks in advance for you input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一指流沙 2024-08-26 07:21:23

使用这个通用的 addLoadEvent 函数...

function addLoadEvent(func) {
  if(typeof window.onload != 'function')
    window.onload = func;
  else {
    var oldLoad = window.onload;

    window.onload = function() {
      if(oldLoad) oldLoad();
      func();
    }
  }
}

这本质上是对要执行的函数进行排队。它永远不会覆盖先前分配的处理程序。下面的示例用法...

addLoadEvent(function() { alert("One!"); });

addLoadEvent(two);
function two() {
  alert("Two!");
}

我想提一下,像 jQuery 这样的库会为您解决此类已知问题。

Use this generic addLoadEvent function...

function addLoadEvent(func) {
  if(typeof window.onload != 'function')
    window.onload = func;
  else {
    var oldLoad = window.onload;

    window.onload = function() {
      if(oldLoad) oldLoad();
      func();
    }
  }
}

This essentially queues up functions to be executed. It never overwrites a previously assigned handler. Sample usage below...

addLoadEvent(function() { alert("One!"); });

addLoadEvent(two);
function two() {
  alert("Two!");
}

I want to mention that libraries like jQuery take care of known issues like this for you.

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