从外部js文件加载
我正在编写一个 js 脚本,人们可以通过将一行代码添加到 HTML 正文部分的标头或末尾来将其添加到他们的网站中。
我的问题是如何在外部 js 文件上执行 onload。 下面的代码可以工作吗? 它不可能在文档加载后运行并错过加载事件吗?
function c_onload () { alert ('onload'); }
if (window.attachEvent) {window.attachEvent('onload', c_onload);}
else if (window.addEventListener) {window.addEventListener('load', c_onload, false);}
else {document.addEventListener('load', c_onload, false);}
(我不能使用 Jquery 或任何其他库)
I'm writing a js script that people will add to their web site by adding a single line of code to the header or end of the body part of their HTML.
My question is how to do the onload right on the external js file. Will the code below work? Can't it possibly run after the onload of the document and miss the onload event?
function c_onload () { alert ('onload'); }
if (window.attachEvent) {window.attachEvent('onload', c_onload);}
else if (window.addEventListener) {window.addEventListener('load', c_onload, false);}
else {document.addEventListener('load', c_onload, false);}
(I can't use Jquery or any other library)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的最后一个
else
子句是做什么用的? 这是相当无用的,恕我直言。
以下应该是一个跨浏览器解决方案:它首先检查
addEventListener()
,然后检查attachEvent()
并回退到onload = ...
代码>另外,kgiannakakis 所说的
事实并非如此:所有主流浏览器都以相同的方式处理
window.onload
,即在加载外部资源(包括外部脚本)后执行侦听器函数。 问题在于DOMContentLoaded
- 这就是使用doScroll()
、defer
、onreadystatechange
以及其他人进行黑客攻击的地方已经煮好了过来玩。根据您的目标受众,您可能想要删除后备代码,甚至只使用它。 我的投票赞成放弃它。
What is your last
else
-clausefor? It's rather useless, imho.
The following should be a cross-browser solution: It first checks for
addEventListener()
, thenattachEvent()
and falls back toonload = ...
Also, what kgiannakakis stated
is not true: all major browsers handle
window.onload
the same way, ie the listener function gets executed after the external resources - including your external script - have been loaded. The problem lies withDOMContentLoaded
- that's where the hacks withdoScroll()
,defer
,onreadystatechange
and whatever else someone has cooked up come to play.Depending on your target audience, you may either want to drop the fallback code or even use it exclusively. My vote would go for dropping it.
恐怕如果您不能使用 jQuery 或其他一些库,您需要重现它们的大部分功能。 原因是浏览器对 onLoad 事件的处理方式不同。
我建议你下载jQuery的代码,看看documentready函数是如何实现的。
I am afraid that if you can't use jQuery or some other library you need to reproduce a way good deal of their functionality. The reason is that browsers handle the onLoad event differently.
I recommend that you download jQuery's code and see how the documentready function is implemented.
onLoad 事件应该在加载其附加的元素时运行。 但有些浏览器* 将此错误解释为“加载前”或“加载期间的某个时间”,因此确保加载所有 html 后运行某些内容的最安全选择是在 HTML 源代码的底部添加对该函数的调用,如下所示:
(*至少某些版本的 Windows 版 Safari 确实存在此问题)
The onLoad event is supposed to be run when the element it is attached to is loaded. But some browsers* misinpret this as "beforeload" or "sometime during load" so the safest option to be sure something is run after all html is loaded, is to add a call to the function on the bottom of the HTML source, like this:
(* at least some versions of Safari for Windows I do beleave have this issue)