Issue with order of execution

P

PaPa

I'm working on a web based presentation of my basic HTML Adult
Education course.

In the middle of the page, I use some javascript to open a window.

<script type="text/javascript">
var fam5=window.open("fam5.htm","fam5", 'width=500 height=500,
scrollbars=1, location=1, menubar=1, resizable=1, toolbar=1, left=20,
top=20');
</script>

fam5.htm is a simple web page that contains a DIV with the id,
"maindiv". I wanted to form a string variable with maindiv's
innerHTML. So, after a lot of irrelevant html, near the end of the
program I put these lines in to see if it would work.

<script type="text/javascript">
var str5=fam5.document.getElementById("maindiv").innerHTML;
alert(str5);
</script>


When I ran the presentation, the window (fam5) opened as I expected,
but I did not get an alert box. My Mozilla Error Console reads:

Error: fam5.document.getElementById("maindiv") is null

I ran another test to see if I was reaching the last lines of code. I
added a second alert box like this:

<script type="text/javascript">
alert('you made it here')
var str5=fam5.document.getElementById("maindiv").innerHTML;
alert(str5);
</script>

When I reran the presentation, I reached the "you made it here" alert
box, but to my surprise, when I clicked OK, the str5 alert box came on
with the proper string. It appears that, somehow, stopping for the
"you made it here" alert allowed the variable str5 to get set up
properly.

There must be something basic about the timing of events that I am
missing. Can someone point me to an appropriate source of
information?

Please keep it basic for this feeble old brain.

PaPa
 
S

SAM

PaPa a écrit :
I'm working on a web based presentation of my basic HTML Adult
Education course.

In the middle of the page, I use some javascript to open a window.

try somenting like :

<script type="text/javascript">

var count = 0,
seen = false;

function myTest() {
var attr = 'width=500,height=500,scrollbars=1,location=1,menubar=1,' +
'resizable=1,toolbar=1,left=20,top=20';
if(typeof fam5 == 'undefined' || fam5.closed)
fam5 = window.open("","", attr);
fam5.onload = function() { seen = true; };
fam5.location = 'fam5.htm';
fam5.focus();
verif();
}

function verif() {
if(!seen) {
count++;
setTimeout(verif, 50);
}
else
alert('count = ' + count +
'\ndiv = '+fam5.document.getElementById('maindiv').innerHTML);
}

myTest();

</script>

------------------
You can also try :

<script type="text/javascript">

var count = 0; // le compteur

function myTest() {
var attr = 'width=500,height=500,scrollbars=1,location=1,menubar=1,' +
'resizable=1,toolbar=1,left=20,top=20';
if(typeof fam5 == 'undefined' || fam5.closed)
fam5 = window.open("","", attr);
fam5.location = 'fam5.htm';
fam5.focus();
verif();
}

function verif() {
if(!fam5.document.getElementById('maindiv')) {
count++;
setTimeout(verif, 50);
}
else
alert('temps d\'attente = ' + count * .5 + ' centièmes' +
'\ndiv = '+fam5.document.getElementById('maindiv').innerHTML);
}

myTest();

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top