最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is window.event in JavaScript? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Apr 15, 2017 at 2:11 Web_Designer 74.6k93 gold badges209 silver badges266 bronze badges asked Jul 21, 2015 at 16:02 mfaieghimfaieghi 6102 gold badges10 silver badges26 bronze badges 3
  • Neither of those exist, except for some old IE versions. – SLaks Commented Jul 21, 2015 at 16:05
  • developer.mozilla/en-US/docs/Web/API/Window – wiretext Commented Jul 21, 2015 at 16:06
  • MDN advises against using window.event. – Dan Dascalescu Commented Oct 9, 2019 at 3:52
Add a ment  | 

4 Answers 4

Reset to default 6

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 patibility. 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 bine 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);
}

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./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).

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 patibility.

Thank you.

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;
}
发布评论

评论列表(0)

  1. 暂无评论