| Page 1 of 1 | [ 10 posts ] |
|
HI,
I am stuck in a very strange problem. I am getting zero from Code: YAHOO.util.Event.getListeners(myelement); even I am registering listeners through Code: YAHOO.util.Event.addListener("myelementid", "click", fnCallback) . Even after getting called Code: YAHOO.util.Event.removeListener("myelementid", "click"); listener is getting called!!! Please let me know what can be cause of this problem. Thanks. |
|
Try changing:
Code: YAHOO.util.Event.getListeners(myelement); To: Code: YAHOO.util.Event.getListeners("myelement"); The first one is looking for an object. The second is looking for an ID. I'm guessing that's what's going on since the other two lines you posted are using the ID version. Also, watch out for the fact that getListeners returns an object, but it can also return null. You'll probably want to test for that. For example: Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Listener test</title> </head> <body> <p> Status: <span id="theStatus">Not clicked yet</span><br /> <a href="#" id="myelementid">click me</a> </p> <p>"myelementid" listeners = <span id="numOfListeners">Not yet set</span><p> <!-- YUI Call --> <script type="text/javascript" src="http://yui.yahooapis.com/2.8.1/build/yahoo-dom-event/yahoo-dom-event.js"></script> <script> (function() { // define variables that will be used var listeners, listenerCount; // define the funtcion callback function fnCallback(e) { // do something useful here: YAHOO.util.Dom.get("theStatus").innerHTML = "Caught a click."; // This will prevent the URL from chaning (i.e. getting a "#" appended) YAHOO.util.Event.preventDefault(e); } // add the listener YAHOO.util.Event.addListener("myelementid", "click", fnCallback) // get object containing the number of listeners listeners = YAHOO.util.Event.getListeners(myelementid); // if there were no listeners assigned, the above var will be null // so set the count to 0 if(listeners === null) { listenerCount = 0; } // otherwise, set it to the number of attached listeners else { listenerCount = listeners.length; } // display the number of listers YAHOO.util.Dom.get("numOfListeners").innerHTML = listenerCount; })(); </script> </body> </html> |
|
Thanks for the reply.
Let me tell you the real scenario I am working on... There is a main page. On clicking a button on this page, a child window opens. On this window, there is a single image without any id. While loading this window, I am getting this image through getElementsByTagId and then registering the listener. There is another button to disable the listener on this image. When I click on that button, I am using the below code for getting the listeners registered on that image... YAHOO.util.Event.getListeners(imgReference) but it is returning null!!! I don't know what's going on. Which DOM it is searching on? Are parent dom and child dom different? |
|
You should post a link. getListeners will return the listeners that YUI registered -- if you have YUI in both frames, a YUI instance will not know about events registered by the other.
|
|
Then how to have a single YUI instance for both parent and child frames? Every time I am using the YAHOO.util.Event, is that mean it is creating different instances of YUI? Please provide the example.
Posting link is not possible currently as it wont be accessible in external network. |
|
You can post code to one of the many code sharing sites. I can't help you without it.
|
|
adam wrote: You should post a link. getListeners will return the listeners that YUI registered -- if you have YUI in both frames, a YUI instance will not know about events registered by the other. Hi, Here is the code. I created two samples that is matching exactly with my scenario... parentFrame.html Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <script src="http://yui.yahooapis.com/2.8.1/build/yahoo/yahoo-min.js" ></script> <script src="http://yui.yahooapis.com/2.8.1/build/event/event-min.js" ></script> <script> function displayListeners(){ var frmref = document.getElementById('myframe'); alert('frmrefrence:' + frmref); var elmnt = frmref.contentWindow.document.getElementById('myimage') alert('elmnt in parent: ' + elmnt); var listeners = YAHOO.util.Event.getListeners(elmnt); alert('listeners: ' + listeners); //alert(listeners.length); } </script> </HEAD> <BODY> <iframe id='myframe' src='imgListenerTest.html' height='70%' width='70%'> </iframe> <input type='button' value='parentButton' title='parentButton' onclick='displayListeners();'/> </BODY> </HTML> imgListenerTest.html Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <script src="http://yui.yahooapis.com/2.8.1/build/yahoo/yahoo-min.js" ></script> <script src="http://yui.yahooapis.com/2.8.1/build/event/event-min.js" ></script> <script> function registerListerners(){ alert('registerListerners'); var elmnt = document.getElementById('myimage'); YAHOO.util.Event.addListener(elmnt, "click", helloWorld); } function helloWorld(e) { alert("Hello World!"); } </script> </HEAD> <BODY onload="registerListerners()"> <table> <tr> <td id='mytd'> <img id='myimage' src="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png" onclick=' return directClicked()' height='50%' width='50%' /> </td> </tr> </table> </BODY> </HTML> In the 2nd html, i am registering listener through YUI and in the 2nd file I am checking the listener. That is returning null!!! I am sure, now you can help me. Thanks. |
|
Hi,
I have posted the sample code for my problem that you asked earlier. Please reply. adam wrote: You should post a link. getListeners will return the listeners that YUI registered -- if you have YUI in both frames, a YUI instance will not know about events registered by the other. |
|
call getListeners with the YAHOO object in the child frame (the one you registered the listeners with), not the one in the parent frame.
|
|
Could you please give me a sample code to do it?
|
| Page 1 of 1 | [ 10 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group