What is window.event in JavaScript?

半腔热情 提交于 2021-02-16 19:21:38

问题


I just can't understand what window.event() does in JavaScript. It is used without any definition. Same thing for document.event(). I also don't understand the difference between these two. Do they accept any arguments?


回答1:


An event is something that is called when something happens, so for example click and keypress are events.

The reason however for window.event() is for cross browser compatibility. So here is some javascript:

object.onclick = function(e) {
  // e holds all of the properties of the event
  // You can access the event properties like so e.target
}

Internet Explorer however, doesn't deal with JavaScript the way other browsers do. So for Internet Explorer to deal with the same code as above we would write something on the lines of

object.onclick = function() {
  alert(window.event.srcElement); // Same as e.target
}

Or you can combine them both together like so:

object.onclick = function(e) {
  e = e || window.event; // Use e if it exists or e will be equal to window.event
  var target = e.target || e.srcElement; // We then use the e.target property but if that doesn't exist we use e.srcElement
  alert(target);
}



回答2:


HTML 4 added the ability to let events trigger actions in a browser, like starting a JavaScript when a user clicks on an element.

Here you can check a list of events that might be useful

I don't know if window.event is used now a days. It seems to be a very generic one. Rather you can use more specifics window events like onerror, onload, onresize, onstorage etc

Before using them be sure to check for browser compatibility.

Thank you.




回答3:


The "informal" window.event is not a "method" of "window", and thus is not called with parenthesis and/or arguments. Instead it is a Microsoft IE only informal global browser "property" of Window used to locate the current active event object (only one event can be active at a time since browsers are single threaded). For more on what an event object might offer, see http://www.w3schools.com/jsref/dom_obj_event.asp

Window.event is only needed before IE9, since IE9+ and other browsers pass the event object as the first parameter to the event handler that has been registered. A standard browser handler "function onclick(e)" would access information about the current event by using the passed in local parameter "e" (or whatever the handler author named it), while prior to IE9 that handler's parameter "e" would be undefined requiring the handler to access window.event instead.

In practice, this means that when handling an event, "old IE" tolerant code examines the "event" passed in and if not found looks for window.event: function handler(e) {var e=e?e:window.event;}

To try and be as helpful as possible, as far as I am aware (and can find), window.event is informally accepted as part of IE but document.event is not and thus would only work through even less than informal IE tolerance (and is even more likely to vary based on IE version, if it ever worked at all).




回答4:


You can handle events using:

htmlElementObj.onclick = function(e) {
 //e->event object
};

However Internet Explorer doesn't pass event object to handler. Instead you can use window.event object which is being updated immediately after the event was fired.

htmlElementObj.onclick = function(e) {
  e = e || window.event;
}


来源:https://stackoverflow.com/questions/31544108/what-is-window-event-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!