Can u plz find the bug in the code ??

I am trying to count the number of box tags in the newly opened window... Inspite of the fact there are 3 box tags it shows 0.
Later I added a small piece of code to the javascript file and it showed 3...

Where's the bug in the code ??
Below are the javascript code and the xul code resp..

the javascript file is :

function openPopupMenu(){

mywind=
window.open( "chrome://window/content/style.xul", "Message", "chrome,scrollbars=1,height=300,width=800");

alert(mywind.document); // without this line it shows 0... with this it's 3 ??

var len = mywind.document.getElementsByTagName('box');

alert(len.length);

}

the xul file is :

style.xul

<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window id="example-window" title="Example 2.2.1"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >

<popupset>
<menupopup id="clipmenu">
<menuitem label="Cut"/>
<menuitem label="Copy"/>
<menuitem label="Paste"/>
</menupopup>
</popupset>

<box id='boxid' flex="1">
</box>

<box id='x'>
</box>

<box id='y'>
</box>

</window>
[1509 byte] By [gsatyaprakash] at [2007-11-20 11:09:19]
# 1 Re: Can u plz find the bug in the code ??
The reason is because the JavaScript is executing before the page even loads. So in reality, there are no <box> tags at the time of that script executing. When you add the alert, it gives time for the page to load. So, what you need to do is tell it to run onload.

<script type="text/javascript">
function countBoxes(){
...
}

window.onload = countBoxes;
</script>
PeejAvery at 2007-11-8 0:43:17 >
# 2 Re: Can u plz find the bug in the code ??
Thank You...
gsatyaprakash at 2007-11-8 0:44:15 >
# 3 Re: Can u plz find the bug in the code ??
You're welcome! Good luck. :wave:
PeejAvery at 2007-11-8 0:45:19 >