Donnerstag, November 14, 2013

AjaxEngine: get rid of event.srcElement

It’s time to say goodbye to some of the compatibility tricks in the AjaxEngine. Here is the first article on maintaining your Javascript code if you come from some older versions.

A note for the future

If time is beyond April 2014 (the time when Windows XP and IE 8 and older dies), you don’t need to read and understand. If you find a code that reads obj.srcElement, just change it to obj.target.
You stumbled upon some old-browser-compatibility issue.

History

The srcElement property on event objects was introduced by Microsoft to find out what html element was actually meant when a event was fired. The AjaxEngine then implemented some tricky code to make this property available in other browser too:

  // enable using evt.srcElement in Mozilla/Firefox
  Event.prototype.__defineGetter__("srcElement", function () {
    var node = this.target;
    while (node.nodeType != 1) node = node.parentNode;
    // test this:
    if (node != this.target) alert("Unexpected event.target!")
    return node;
  });

Now, the W3C standard defines a target property that has the same meaning and also Microsoft moves to use the standard conform property in the newer browsers. I strongly advise to not support these older browsers any more because they never will be fixed for security issues any more.

For now you should find the target object inside an event handler by using:

var obj = evt.target || evt.srcElement;

and starting with April 2014 you just should use:

var obj = evt.target;

Changes to AjaxEngine

I searched all “.srcElement” occurrences and changed them to the compatible assignment

var obj = evt.target || evt.srcElement;
An updated download archive is available as well as commits to the sourcecode repository:
http://sourceforge.net/projects/ajaxengine/
or directly using subversion:
https://svn.code.sf.net/p/ajaxengine/code/trunk
See also

http://msdn.microsoft.com/en-us/library/ie/ff974945(v=vs.85).aspx