What is the meaning of this…“var evt=event||window.event;”

前端 未结 3 1186
别跟我提以往
别跟我提以往 2020-12-15 07:07

What does the following mean in JavaScript?

var evt=event||window.event;
3条回答
  •  孤城傲影
    2020-12-15 07:37

    The code is a hack because Microsoft decided to put their events in the global window.event instead of passing it as a parameter to the event function.

    So, this code will attempt to set evt to the event passed in (which will work for the non-Microsoft browsers) and, if that turns out to be null (as it will be for Microsoft browsers), it will then grab it from the global.

    From that point on, your function can just use evt disregarding browser differences (well, at least those relating to events).

提交回复
热议问题