开发者

Explanation on this Javascript check

开发者 https://www.devze.com 2023-03-09 17:31 出处:网络
function doSomething(e) { var targ; if (!e) var e = window.event; //<<<< what does it do this check?
function doSomething(e) {
    var targ;
    if (!e) var e = window.event; //<<<< what does it do this check?
    if (e.target) targ = e.target;
}

Wh开发者_JS百科y do we need to check this?


This adds compatibility with older (Internet Explorer?) browsers that didn't support passing the event object to handlers but instead defined them on the window object.

Also the var is not needed in var e = window.event because it's already declared (parameter).

It can also be written as e = e || window.event;


Basically this if checks if e variable is set. If it isn't - it is assigned a value of window.event (the window event that happened)

0

精彩评论

暂无评论...
验证码 换一张
取 消