evt.target" and "window.event.srcElement".

">
立诚勿怠,格物致知
It's all about connecting the dots

evt and window.event in JavaScript

Today, in codes for a BINGO game, I saw a JavaScript code snippet as below.


If (evt) {
    var thisSquare = evt.target;
}
else {
    var thisSquare = window.event.srcElement;
}

You may want to ask what “thisSquare” here means. A good question! You know, typically, a BINGO game is played on a 5 rows * 5 columns layout (therefore there are 25 cells in total). The above code is aimed to fetch the information that which cell is clicked by the mouse (so you now know “thisSquare” refers to the currently clicked cell). However, the meaning of “thisSquare” is not my concern. I paid much attention to the contents after the equality sign, i.e. “evt.target” and “window.event.srcElement“.

  • If a value called evt is existed, it can be sure that the code is running in a non-IE browser, and we can look at its target to determine which object is influenced by the event.
  • If the code is running in a IE browser, then the evt is not existed. Instead we should look at the event property of the window object, and look at its srcElement property.

Actually, the above code snippet can be written by a ternary if operator employing the inline if syntax as below:


var thisSquare = (evt) ? evt.target : window.event.srcElement;

Note: evt is not a boolean. If evt isn’t null, underfined, NaN (not sure), 0 or false, it’s considered truthy. Basically, if evt has been set already. it’s truthy.

赞(0) 打赏
版权声明:非商业用途转载请注明文章链接,商业用途转载请联系邮箱获取授权。
文章名称:《evt and window.event in JavaScript》
文章链接:https://www.orzzone.com/evt-and-window-event-in-javascript.html
商业联系:yakima.public@gmail.com
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

大前端WordPress主题 更专业 更方便

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

去看看

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力提供更多优质内容!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册