473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Event handling details in IE

Dormilich
8,658 Expert Mod 8TB
Currently I’m writing on an Event handler for IE (including capturing, which hardly any cross browser handler does), but I can’t test my code in IE (I don’t have Windows).

My particular question is about the following:
Expand|Select|Wrap|Line Numbers
  1. base = e.target || e.srcElement;
  2. if (base == this) { // would this work?
  3.     // execute capture handlers
  4. }
This code snippet is executed in the handler attached to the onevent properties, so this points to the appropriate element.

PS. works in FF (if I hide .addEventListener())
May 4 '10 #1

✓ answered by gits

the problem is the getParents() method ... could be fixed with:
Expand|Select|Wrap|Line Numbers
  1. function getParents(elem) {
  2.     var p = [];
  3.  
  4.     while (elem.nodeType == 1) {
  5.         p.push(elem);
  6.         elem = elem.parentNode;
  7.     }
  8.  
  9.     return p;
  10. }
obviously elem instanceof Element doesn't work in IE ... with the above fix the alerts are as expected ...

kind regards

17 2221
RamananKalirajan
608 512MB
Can I know from where you are getting or referring 'this' object. Are you adding any listener for any form element or object?

Thanks and Regards
Ramanan Kalirajan
May 7 '10 #2
Dormilich
8,658 Expert Mod 8TB
Can I know from where you are getting or referring 'this' object.
as it is usual for event handlers, this is assigned automatically to the element that the handler is executed on.

Are you adding any listener for any form element or object?
I can add add any listener to any Element. the only constraint for the object would be that I’ll prototype the "addEvent" function as "addEventListener" into the Element interface.

give word, if you need to see the whole function
May 7 '10 #3
RamananKalirajan
608 512MB
Yes I need to see the function.. because in IE it is expecting the 'this' object when i tried it.

Thanks and Regards
Ramanan Kalirajan
May 7 '10 #4
Dormilich
8,658 Expert Mod 8TB
I guess you need the addEvent() function too …

Expand|Select|Wrap|Line Numbers
  1. // function that executes events according to DOM
  2. function IEHandler(e)
  3. {
  4.     // if no Event object is passed (IE)
  5.     e = e || window.event;
  6.  
  7.     var base      = e.target || e.srcElement, // get Event Target
  8.         evTypeRef = '__' + e.type, // get Event Type
  9.         retValue  = true, // return values for bubbling handlers
  10.         i, j, l, elem, chain, evPhase;
  11.  
  12.     // execute only once
  13.     if (base == this) {
  14.         // get elements for capturing
  15.         chain = getParents(base); // a simple while() loop getting the .parentNode
  16.         for (i = chain.length; i--;) {
  17.             elem = chain[i];
  18.             // if there are functions attached to execute in capturing context
  19.             if (elem[evTypeRef] && elem[evTypeRef].capture) {
  20.                 evPhase = elem[evTypeRef].capture;
  21.                 // execute each function
  22.                 for (j = 0, l = evPhase.length; j < l; j++) {
  23.                     if (evPhase[j]) {
  24.                         // there can be no value returned due to the executing
  25.                         // element of this loop is the Event target and not the
  26.                         // element the handler executes on. 
  27.                         callHandler(evPhase[j], elem, e); // work around .call(), if not supported
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.     }
  33.     // if there are functions attached to execute in bubbling context
  34.     if (this[evTypeRef] && this[evTypeRef].bubble) {
  35.         evPhase = this[evTypeRef].bubble;
  36.         for (j = 0, l = evPhase.length; j < l; j++) {
  37.             if (evPhase[j]) {
  38.                 // this time we're in the correct element context so we can
  39.                 // safely return something
  40.                 retValue = callHandler(evPhase[j], this, e) && retValue;
  41.             }
  42.         }
  43.     }
  44.     return retValue;
  45. }
  46.  
  47. function addEvent(obj, evType, fn, useCapture) 
  48. {
  49.     if (!(fn instanceof Function)) {
  50.         throw new TypeError("Function expected!");
  51.     }
  52.     // make useCapture a Boolean
  53.     useCapture = !!useCapture;
  54.  
  55.     // W3C
  56.     if (obj.addEventListener) {
  57.         obj.addEventListener(evType, fn, useCapture);
  58.     } 
  59.     else {
  60.         var evTypeRef = '__' + evType,
  61.             phase     = useCapture ? "capture" : "bubble";
  62.  
  63.         // create ".__event" property
  64.         if (!obj[evTypeRef]) {
  65.             obj[evTypeRef] = {};
  66.         }
  67.         // search in ".__event.bubble"/".__event.capture"
  68.         // if function is already registered
  69.         if (obj[evTypeRef][phase]) {
  70.             if (array_search(fn, obj[evTypeRef][phase]) > -1) { // work around .indexOf(), if not supported
  71.                 return;
  72.             }
  73.         }
  74.         // create ".__event.bubble"/".__event.capture" property
  75.         else {
  76.             obj[evTypeRef][phase] = [];
  77.             if (!useCapture && obj['on' + evType] && obj['on' + evType] != IEHandler) {
  78.                 // add any previous function assigned to .onevent
  79.                 obj[evTypeRef][phase][0] = obj['on' + evType];
  80.             }
  81.         }
  82.         // add function to stack
  83.         obj[evTypeRef][phase].push(fn);
  84.         // set "global" handler
  85.         obj['on' + evType] = IEHandler;
  86.     }
  87. }
May 7 '10 #5
gits
5,390 Expert Mod 4TB
Hi Domilich,

basically the line:
Expand|Select|Wrap|Line Numbers
  1. if (base == this) { 
should work ... since
Expand|Select|Wrap|Line Numbers
  1. base = e.target || e.srcElement
retrieves the reference to the node where the event was captured and triggered the corresponding handler. currently i don't see where a problem could be? what could be unsure?

kind regards
gits

PS: you might even have an additional look here at the 'Which HTML element is the target of the event?'-section ... where they have an additional Safari-hack ... don't know whether this would be relevant today?
May 7 '10 #6
Dormilich
8,658 Expert Mod 8TB
currently i don't see where a problem could be? what could be unsure?
my problem is that I don’t have an IE to test it. in theory it should work fine …
May 7 '10 #7
gits
5,390 Expert Mod 4TB
... is there simple testcase that you might provide ... so that i could just call it with IE ... at home i might use an IE4linux ... but for the Safari issue that was mentioned in the above post on quirksmode.org i couldn't test it ... just dropped my last and good old power-mac a half year ago ... so i don't have a real safari to test ... just a chromium on linux which is even webKit based and should work similar ...

kind regards
May 7 '10 #8
Dormilich
8,658 Expert Mod 8TB
I don’t need to test the code in Safari/Chrome/FF/Opera, since those support addEventListener.

test link
May 8 '10 #9
gits
5,390 Expert Mod 4TB
it alerts 3 and then 4 ... in IE7 ...

kind regards

PS: the mentioned Safari issue doesn't concern the adding of events ... just the retrieval of the correct target ... but it was just an additional note ...
May 8 '10 #10
Dormilich
8,658 Expert Mod 8TB
it alerts 3 and then 4 ... in IE7 ...
any idea why it skipped the capturing part?
May 8 '10 #11
gits
5,390 Expert Mod 4TB
IE uses event-bubbling according to this - section: Event Flow - and i think that IE just supports bubbling, while more W3C compliant browsers support both, capturing and bubbling. don't know whether IE could be forced to use capturing ... there seems to be a setCapture() method ... but i didn't investigate on that ... don't really think that it would be worth to fiddle with that :)

kind regards
May 8 '10 #12
Dormilich
8,658 Expert Mod 8TB
I know that IE only supports bubbling, that's why I added a code block (post #5, lines 13 - 32), that traverses the Nodes from window to the target Element. The only problem is, that this block must be executed only once, which would be the point just before IE starts bubbling (or calling the functions on the bubble handlers).

my assumption is that either if (base == this) doesn't work or something's wrong with the parent element chain.
May 8 '10 #13
gits
5,390 Expert Mod 4TB
started to debug it: if (base == this) works as intended ... will have a closer look now ...
May 9 '10 #14
Dormilich
8,658 Expert Mod 8TB
I modified the getParents() function, maybe it works now.
May 9 '10 #15
gits
5,390 Expert Mod 4TB
the problem is the getParents() method ... could be fixed with:
Expand|Select|Wrap|Line Numbers
  1. function getParents(elem) {
  2.     var p = [];
  3.  
  4.     while (elem.nodeType == 1) {
  5.         p.push(elem);
  6.         elem = elem.parentNode;
  7.     }
  8.  
  9.     return p;
  10. }
obviously elem instanceof Element doesn't work in IE ... with the above fix the alerts are as expected ...

kind regards
May 9 '10 #16
Dormilich
8,658 Expert Mod 8TB
obviously elem instanceof Element doesn't work in IE
good to know, but somehow I’m not surprised …
May 9 '10 #17
gits
5,390 Expert Mod 4TB
crappy IE ... as always :) ...
May 9 '10 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
3
by: Lachlan Hunt | last post by:
Hi, I've been looking up lots of documentation and trying to work out a cross-platform method of capturing a keyboard event and working out which key was pressed. From what I can find, there...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
2
by: ETL | last post by:
Hi, I think this question has an easy answer but I can't seem to find anything online that lists best practices for my problem. I have a C# app that uses an exception handling tree similar to...
5
by: Steve | last post by:
I have a datagrid in a WinForm. When the user edits an entry in the datagrid, after he leaves that field, I would like to do some cheking. What event fires when the user does that? I need to...
17
by: dan_williams | last post by:
I have the following test web page:- <html> <head><title>Test</title> <script language="Javascript"> <!-- function fnTR() { alert("TR"); }
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
31
by: Scott M. | last post by:
Am I correct in thinking that because C# doesn't have the "Handles" keyword that VB .NET does, we have to register event delegates manually in C#, whereas in VB .NET using "Handles" takes care of...
1
by: mesut | last post by:
Hi colleagues, In fact I don't have a problem but just don't know which way is good and better. I need some advice which programming way I shall use. Since a couple of days I'm investing my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.