Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the window.event property and its usage

I don't understand the motivation behind window.event or window.event.srcElement. In what context should one use this? What exactly does it represent in the DOM?

like image 473
Craig Pottinger Avatar asked Aug 03 '11 13:08

Craig Pottinger


People also ask

What is window event?

A low-level event that indicates that a window has changed its status. This low-level event is generated by a Window object when it is opened, closed, activated, deactivated, iconified, or deiconified, or when focus is transfered into or out of the Window.

What are events how window events are handled in JavaScript?

JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc.

Which window event is used to create a new window?

The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.


1 Answers

Not sure if this difference has been changed in newer browser versions but basically, "In the Microsoft event accessing model there is a special property window.event that contains the last event that took place." (from reference)

So, to write an event handler compatible across browsers you'd need to do something like this:

function doSomething(e) {
    if(!e) {
        var e = window.event;
    }
    var ele = e.target || e.srcElement;
    // get the clicked element
    // srcElement for IE, target for others
}
element.onclick = doSomething;

Reference: http://www.quirksmode.org/js/events_access.html

like image 77
potNPan Avatar answered Oct 03 '22 15:10

potNPan