|
From: pamelafluente on 27 Sep 2006 06:23 Ok I am trying to attach the handlers dynamically: (within a JS file: ) ------------------------------------------------------- window.onload = addCellHandlers(); function addCellHandlers() { var foundDivs = document.body.getElementsByTagName("div"); var divElement; for ( var i = 0; i < foundDivs.length; i++ ) { divElement = foundDivs[i]; if ( divElement.getAttribute("hasHandlers") == "True" ) { divElement.onclick="cellClick(event,this)" ; divElement.onmouseover = "mOver(this)" ; divElement.onmouseout = "mOut(this)" ; } } } hmm this gets called, but does not seem to attach any handler. I suspect that foundDivs is empty and I am probably invoking this in the wrong way. Is there some kind of afterLoad event ? How do I correctly add handlers to my DIVs? -P
From: Laurent Bugnion on 27 Sep 2006 06:45 Hi, pamelafluente(a)libero.it wrote: > Ok I am trying to attach the handlers dynamically: > > (within a JS file: ) > ------------------------------------------------------- > > window.onload = addCellHandlers(); Should be window.onload = addCellHandlers; What you want to do is assign a reference to a Function object to the onload property of the Window object. The reference to the Function object must be passed without the (). What you do is call the function, and assign the result (which doesn't exist in that case) to the window.onload property. HTH, Laurent > function addCellHandlers() { > > var foundDivs = document.body.getElementsByTagName("div"); > var divElement; > for ( var i = 0; i < foundDivs.length; i++ ) { > divElement = foundDivs[i]; > if ( divElement.getAttribute("hasHandlers") == "True" ) { > divElement.onclick="cellClick(event,this)" ; > divElement.onmouseover = "mOver(this)" ; > divElement.onmouseout = "mOut(this)" ; > } > } > } > > > hmm this gets called, but does not seem to attach any handler. > > I suspect that foundDivs is empty and I am probably invoking this in > the wrong way. > > Is there some kind of afterLoad event ? > How do I correctly add handlers to my DIVs? > > -P > -- Laurent Bugnion, GalaSoft Software engineering: http://www.galasoft-LB.ch PhotoAlbum: http://www.galasoft-LB.ch/pictures Support children in Calcutta: http://www.calcutta-espoir.ch
From: pamelafluente on 27 Sep 2006 06:49 Hi Laurent. Just tried. Without "()" the function is not invoked. The problem seems to be that the array foundDivs is empty. Why? -P Laurent Bugnion ha scritto: > Hi, > > pamelafluente(a)libero.it wrote: > > Ok I am trying to attach the handlers dynamically: > > > > (within a JS file: ) > > ------------------------------------------------------- > > > > window.onload = addCellHandlers(); > > Should be window.onload = addCellHandlers; > > What you want to do is assign a reference to a Function object to the > onload property of the Window object. The reference to the Function > object must be passed without the (). What you do is call the function, > and assign the result (which doesn't exist in that case) to the > window.onload property. > > HTH, > Laurent > > > function addCellHandlers() { > > > > var foundDivs = document.body.getElementsByTagName("div"); > > var divElement; > > for ( var i = 0; i < foundDivs.length; i++ ) { > > divElement = foundDivs[i]; > > if ( divElement.getAttribute("hasHandlers") == "True" ) { > > divElement.onclick="cellClick(event,this)" ; > > divElement.onmouseover = "mOver(this)" ; > > divElement.onmouseout = "mOut(this)" ; > > } > > } > > } > > > > > > hmm this gets called, but does not seem to attach any handler. > > > > I suspect that foundDivs is empty and I am probably invoking this in > > the wrong way. > > > > Is there some kind of afterLoad event ? > > How do I correctly add handlers to my DIVs? > > > > -P > > > > -- > Laurent Bugnion, GalaSoft > Software engineering: http://www.galasoft-LB.ch > PhotoAlbum: http://www.galasoft-LB.ch/pictures > Support children in Calcutta: http://www.calcutta-espoir.ch
From: pamelafluente on 27 Sep 2006 07:04 pamelafluente(a)libero.it ha scritto: > Hi Laurent. > Just tried. Without "()" the function is not invoked. > > > The problem seems to be that the array foundDivs is empty. Why? > No actually you are right !! Now it works. It's strange that the function is called both with and without "()". But, in the first case the foundDivs remains empty (??) Now there must be some other problems because the handlers do not get attacched to the div. I suspect that: divElement.onclick = "cellClick(event,this)" ; divElement.onmouseover = "mOver(this)" ; divElement.onmouseout = "mOut(this)" ; is a wrong way to do that. What's wrong here? -P
From: Laurent Bugnion on 27 Sep 2006 07:18 Hi, pamelafluente(a)libero.it wrote: > Hi Laurent. > Just tried. Without "()" the function is not invoked. > > > The problem seems to be that the array foundDivs is empty. Why? > > -P I checked your code, and there are multiple problems. - First, related to your other post in that thread, there is no afterLoad event. onload is correct, it will be called after the document has been parsed and interpreted. When onload is called, the DIVs are there. - getElementsByTagName is in document, not document.body --> var foundDivs = document.getElementsByTagName("div"); alert( foundDivs.length ); - You must use window.onload = addCellHandlers; // without () - Similarly, in your function addCellHandlers, you must also assign the event handlers without () and it may not be a string. For example: divElement.onclick = cellClick; divElement.onmouseover = mOver; divElement.onmouseout = mOut; As you'll probably notice, this doesn't allow passing any parameter. Thankfully, JavaScript has closure, which allows you to refer to externally defined variables from inside a function: var thisLocal = this; divElement.onclick = function() { mOver( thisLocal ) }; In that case I create an anonymous function, which I pass to the divElement.onclick property. Note the use of closure: I assigne the content of "this" to a local variable, defined outside of the anonymous function. Then from inside the function, I can refer to that external variable, because the context in which the function was defined is accessible when the function is called. That's closure. - Question: Did you define "hasHandlers" yourself in the server-side code? It's not a standard attribute of DIV. That might cause validation errors because it's not standard HTML, but it's not that big an issue. HTH, Laurent > > Laurent Bugnion ha scritto: > > >>Hi, >> >>pamelafluente(a)libero.it wrote: >> >>>Ok I am trying to attach the handlers dynamically: >>> >>>(within a JS file: ) >>>------------------------------------------------------- >>> >>>window.onload = addCellHandlers(); >> >>Should be window.onload = addCellHandlers; >> >>What you want to do is assign a reference to a Function object to the >>onload property of the Window object. The reference to the Function >>object must be passed without the (). What you do is call the function, >>and assign the result (which doesn't exist in that case) to the >>window.onload property. >> >>HTH, >>Laurent >> >> >>>function addCellHandlers() { >>> >>> var foundDivs = document.body.getElementsByTagName("div"); >>> var divElement; >>> for ( var i = 0; i < foundDivs.length; i++ ) { >>> divElement = foundDivs[i]; >>> if ( divElement.getAttribute("hasHandlers") == "True" ) { >>> divElement.onclick="cellClick(event,this)" ; >>> divElement.onmouseover = "mOver(this)" ; >>> divElement.onmouseout = "mOut(this)" ; >>> } >>> } >>>} >>> >>> >>>hmm this gets called, but does not seem to attach any handler. >>> >>>I suspect that foundDivs is empty and I am probably invoking this in >>>the wrong way. >>> >>>Is there some kind of afterLoad event ? >>>How do I correctly add handlers to my DIVs? >>> >>>-P >>> >> >>-- >>Laurent Bugnion, GalaSoft >>Software engineering: http://www.galasoft-LB.ch >>PhotoAlbum: http://www.galasoft-LB.ch/pictures >>Support children in Calcutta: http://www.calcutta-espoir.ch > > -- Laurent Bugnion, GalaSoft Software engineering: http://www.galasoft-LB.ch PhotoAlbum: http://www.galasoft-LB.ch/pictures Support children in Calcutta: http://www.calcutta-espoir.ch
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: Get parent iframe element in iframe Next: AfterLoad ?? No Div found ? |