[ 10 posts ]

shai ban

  • Username: shai_ban007
  • Joined: Wed Oct 13, 2010 9:35 pm
  • Posts: 6
  • IRC: shai
  • Offline
  • Profile

YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Wed Oct 13, 2010 10:21 pm
+0-
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.

Alan W. Smith

  • Username: StimulatingPixels
  • Joined: Thu Jun 25, 2009 7:56 am
  • Posts: 19
  • Offline
  • Profile

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Mon Oct 18, 2010 6:14 pm
+0-
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>&quot;myelementid&quot; 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>

shai ban

  • Username: shai_ban007
  • Joined: Wed Oct 13, 2010 9:35 pm
  • Posts: 6
  • IRC: shai
  • Offline
  • Profile
Tags:

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Tue Oct 19, 2010 8:52 am
+0-
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?

Adam Moore

YUI Contributor

  • Username: adam
  • Joined: Wed Sep 03, 2008 11:16 am
  • Posts: 356
  • GitHub: apm
  • Gists: apm
  • Offline
  • Profile

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Tue Oct 19, 2010 9:06 am
+0-
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.

shai ban

  • Username: shai_ban007
  • Joined: Wed Oct 13, 2010 9:35 pm
  • Posts: 6
  • IRC: shai
  • Offline
  • Profile
Tags:

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Tue Oct 19, 2010 9:21 am
+0-
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.

Adam Moore

YUI Contributor

  • Username: adam
  • Joined: Wed Sep 03, 2008 11:16 am
  • Posts: 356
  • GitHub: apm
  • Gists: apm
  • Offline
  • Profile

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Tue Oct 19, 2010 9:32 am
+0-
You can post code to one of the many code sharing sites. I can't help you without it.

shai ban

  • Username: shai_ban007
  • Joined: Wed Oct 13, 2010 9:35 pm
  • Posts: 6
  • IRC: shai
  • Offline
  • Profile
Tags:

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Fri Oct 29, 2010 7:45 pm
+0-
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.

shai ban

  • Username: shai_ban007
  • Joined: Wed Oct 13, 2010 9:35 pm
  • Posts: 6
  • IRC: shai
  • Offline
  • Profile

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Mon Nov 08, 2010 10:08 pm
+0-
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.

Adam Moore

YUI Contributor

  • Username: adam
  • Joined: Wed Sep 03, 2008 11:16 am
  • Posts: 356
  • GitHub: apm
  • Gists: apm
  • Offline
  • Profile

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Tue Nov 09, 2010 10:56 am
+0-
call getListeners with the YAHOO object in the child frame (the one you registered the listeners with), not the one in the parent frame.

shai ban

  • Username: shai_ban007
  • Joined: Wed Oct 13, 2010 9:35 pm
  • Posts: 6
  • IRC: shai
  • Offline
  • Profile

Re: YAHOO.util.Event.getListeners is returning zero!!!

Post Posted: Wed Nov 10, 2010 3:50 am
+0-
Could you please give me a sample code to do it?
  [ 10 posts ]
Display posts from previous:  Sort by  
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