从外部js文件加载

发布于 2024-07-13 21:11:50 字数 455 浏览 5 评论 0原文

我正在编写一个 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 技术交流群。

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

发布评论

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

评论(3

触ぅ动初心 2024-07-20 21:11:50

你的最后一个else子句是做什么

else {document.addEventListener('load', c_onload, false);

用的? 这是相当无用的,恕我直言。

以下应该是一个跨浏览器解决方案:它首先检查 addEventListener(),然后检查 attachEvent() 并回退到 onload = ...代码>

function chain(f1, f2) {
    return typeof f1 !== 'function' ? f2 : function() {
        var r1 = f1.apply(this, arguments),
            r2 = f2.apply(this, arguments);
        return typeof r1 === 'undefined' ? r2 : (r1 && r2);
    };
}

function addOnloadListener(func) {
    if(window.addEventListener) 
        window.addEventListener('load', func, false);
    else if(window.attachEvent)
        window.attachEvent('onload', func);
    else window.onload = chain(window.onload, func);
}

另外,kgiannakakis 所说的

原因是浏览器处理 onLoad 事件的方式不同。

事实并非如此:所有主流浏览器都以相同的方式处理 window.onload,即在加载外部资源(包括外部脚本)后执行侦听器函数。 问题在于 DOMContentLoaded - 这就是使用 doScroll()deferonreadystatechange 以及其他人进行黑客攻击的地方已经煮好了过来玩。


根据您的目标受众,您可能想要删除后备代码,甚至只使用它。 我的投票赞成放弃它。

What is your last else-clause

else {document.addEventListener('load', c_onload, false);

for? It's rather useless, imho.

The following should be a cross-browser solution: It first checks for addEventListener(), then attachEvent() and falls back to onload = ...

function chain(f1, f2) {
    return typeof f1 !== 'function' ? f2 : function() {
        var r1 = f1.apply(this, arguments),
            r2 = f2.apply(this, arguments);
        return typeof r1 === 'undefined' ? r2 : (r1 && r2);
    };
}

function addOnloadListener(func) {
    if(window.addEventListener) 
        window.addEventListener('load', func, false);
    else if(window.attachEvent)
        window.attachEvent('onload', func);
    else window.onload = chain(window.onload, func);
}

Also, what kgiannakakis stated

The reason is that browsers handle the onLoad event differently.

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 with DOMContentLoaded - that's where the hacks with doScroll(), 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.

慢慢从新开始 2024-07-20 21:11:50

恐怕如果您不能使用 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.

感情废物 2024-07-20 21:11:50

onLoad 事件应该在加载其附加的元素时运行。 但有些浏览器* 将此错误解释为“加载前”或“加载期间的某个时间”,因此确保加载所有 html 后运行某些内容的最安全选择是在 HTML 源代码的底部添加对该函数的调用,如下所示:

    ...
        <script type="text/javascript">
            c_onload();
        </script>
    </body>
</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:

    ...
        <script type="text/javascript">
            c_onload();
        </script>
    </body>
</html>

(* at least some versions of Safari for Windows I do beleave have this issue)

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