简体   繁体   English

onload = function vs window.onload = function

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

Are there any real advantages to using window.onload=function(){}; 使用window.onload = function(){}是否有任何实际优势; over 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. 我知道window.onload看起来更合适,但这不是我选择它的好理由,特别是它比onload更长/更慢。

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: 经过一些耗时的搜索和测试,这2个是唯一的2个浏览器兼容方法,测试(在相对较新的Chrome / Firefox版本和IE从5.5到9)包括:

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/ http://perfectionkills.com/onloadfunction-considered-harmful/

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). 它指出ECMA-262第5版的严格模式( “使用严格”;我不打算在我的项目中使用)可能最终导致某些浏览器与onload不兼容(Firefox和Opera中的ReferenceError)。

So the question is: are there any real disadvantages related to using the direct onload assignment other than the "use strict;" 所以问题是:除了“use strict”之外,使用直接onload赋值是否有任何真正的缺点? one? 一? I need infos not some unexplained opinions. 我需要的信息不是一些无法解释的意见。

Thanks 谢谢

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. 注意:我在问这个问题之前做过搜索(看起来有点经典),我能找到的最接近的问题是关于window.onload vs <body onload =“”>window.onload的其他替代方法等。

Edit: I've created this test case onload vs window.onload , which proves how faster is onload. 编辑:我已经创建了这个测试用例onload vs window.onload ,这证明了onload的速度有多快。 I would really go for this kind of micro optimisations because why not? 我真的会去做这种微观优化,为什么不呢? They can be cool sometimes. 它们有时可能很酷。

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. 两者都是相同的...当你自己调用onload然后javascript假定它是一个全局属性,它是window对象的属性。 So basically if you don't specifically say it's window.onload then the javascript engine will do it for you. 所以基本上如果你没有特别说它是window.onload那么javascript引擎会为你做。

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

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. 但是,使用完整的window.onload而不仅仅是onload被认为更好。 You're not gaining much by not typing the extra 7 characters. 没有输入额外的7个字符,你获得的收益并不高。

All global functions and variables are inside window object. 所有全局函数和变量都在window对象中。 So when you are using onload you are using window.onload . 因此,当您使用onload您正在使用window.onload

When calling something inside a namespace you have a performance cost: it has to get the object window and then the property onload. 在命名空间内调用某些内容时,您会有性能成本:它必须获取对象窗口,然后是属性onload。

When calling a global variable without window you have the risk that it is redefined inside its scope: 在没有窗口的情况下调用全局变量时,您可能会在其范围内重新定义它:

function () {
  var onLoad = function() {alert("foo")};
  function () {
    onLoad();
  }
}

In this example onload alerts "foo"; 在这个例子中,onload警告“foo”;

So you asked are there any real disadvantages related to using the direct onload assignment : 所以你问are there any real disadvantages related to using the direct onload assignment

The onload may be redefined and the code may not work. 可以重新定义onload并且代码可能不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM