页面渲染后执行 Javascript IE6
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这个通用的 addLoadEvent 函数...
这本质上是对要执行的函数进行排队。它永远不会覆盖先前分配的处理程序。下面的示例用法...
我想提一下,像 jQuery 这样的库会为您解决此类已知问题。
Use this generic addLoadEvent function...
This essentially queues up functions to be executed. It never overwrites a previously assigned handler. Sample usage below...
I want to mention that libraries like jQuery take care of known issues like this for you.