onload = function vs window.onload = function [英] onload=function vs window.onload=function

查看:87
本文介绍了onload = function vs window.onload = function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用window.onload = function(){}是否有任何真正的好处; over onload = function(){}; ?我知道window.onload看起来更合适,但这不是我选择它的好理由,特别是它比onload更长/更慢。

Are there any real advantages to using window.onload=function(){}; over onload=function(){}; ? I know window.onload looks more proper, but that's not a good reason for me to choose it, especially that it's longer/slower than onload.

经过一段时间的消耗搜索和测试,这2个是唯一的2个浏览器兼容方法,测试(在相对较新的Chrome / Firefox版本,以及从5.5到9的IE)包括:

After some time-consuming searches and tests, those 2 were the only 2 browser compatible methods, the tests (on relatively new Chrome/Firefox versions, and IE from 5.5 to 9) included:

window.onload // works in all tested browsers
onload // works in all tested browsers, faster than window.onload
document.onreadystatechange // works twice in some browsers, once in some others, could be confusing
window.onpageshow // works in chrome and firefox, not in IE
window.onreadystatechange // doesn't work
document.onload // doesn't work
document.onpageshow // doesn't work
window.document.onload // doesn't work

我可以找到这篇文章,这是我提出的最适合的文章之一:

I could find this article which is one of the most suited articles to my question:

http://perfectionkills.com/onloadfunction-considered-harmful/

它声明 ECMA-262第5版的严格模式( use strict; 我不打算在我的项目中使用)可能最终导致某些浏览器与onload不兼容(Firefox和Opera中的ReferenceError)。

It states that the strict mode of ECMA-262 5th edition ("use strict"; which I don't plan to use in my project) could finally cause some browser incompatibility to onload (ReferenceError in Firefox and Opera).

所以问题是:除了use strict之外,使用直接onload赋值是否有任何真正的缺点?一?我需要的信息不是一些无法解释的意见。

So the question is: are there any real disadvantages related to using the direct onload assignment other than the "use strict;" one? I need infos not some unexplained opinions.

谢谢

注意:我做过在问这个问题之前搜索(我看起来有点经典),我能找到的最接近的问题是 window.onload vs< body onload => 其他替代品window.onload 等。

Note: I did search before asking this question (which looks a bit classic I know), the closest questions I could find were about window.onload vs <body onload="">, other alternatives to window.onload, etc.

编辑:我已创建此测试用例 onload vs window.onload ,它证明了onload的速度有多快。我真的会去做这种微观优化,为什么不呢?它们有时很酷。

I've created this test case onload vs window.onload, which proves how faster is onload. I would really go for this kind of micro optimisations because why not? They can be cool sometimes.

推荐答案

两者都是一样的...当你自己调用onload然后javascript假设它是一个全局属性,它是窗口对象的属性。所以基本上如果你没有特别说它是window.onload那么javascript引擎会为你做。

Both are the same... When you call onload by itself then javascript assumes that it's a global property which is a property of the window object. So basically if you don't specifically say it's window.onload then the javascript engine will do it for you.

if (onload === window.onload) {
   alert("it's the same");  //true
}

所以只要你不关心严格模式你现代浏览器应该没有任何问题。但是,使用完整的window.onload而不仅仅是onload被认为更好。没有输入额外的7个字符,你获得的收益并不高。

So as long as you don't care about strict mode you shouldn't have any problem with modern browsers. However it's considered better to use the full window.onload instead of just onload. You're not gaining much by not typing the extra 7 characters.

这篇关于onload = function vs window.onload = function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆