Forum Moderators: open
It only does it for Firefox, in IE there is no problem.
Is there any workaround or other way to get the messages to appear as they happen?
Here is the code of the demo:-
<script>
function wait(ms){ // Wait for ms millisecs
var start = new Date()
while( ((new Date())-start)<ms ) {}
return}
function test() {
w1 = window.open("", 'w1'+(new Date()).getTime(), "width=200,height=200,screenX=100,left=100,screenY=100,top=100");
w1.document.write("First Window<BR>");
w1.document.close()
// Why doesn't Firefox display the text "First Window" before the delay? IE and Netscape 4.79 do.
wait(3000)
w2 = window.open("", 'w2'+(new Date()).getTime(), "width=200,height=200,screenX=200,left=200,screenY=200,top=200");
w2.document.write("Second Window<BR>");
w2.document.close()
// Firefox waits until the end before displaying the text "First Window".
// This means things are getting done out of order.
return}
</script>
<br>
<a href="javascript:void(0)" onClick="test();return false"> Start the test</a><br>
function test() {
window.setTimeout("doWindow(1)", 1); // or just doWindow(1) ?
window.setTimeout("doWindow(2)", 3000);
}
where doWindow has open + write
Problems I see with your code
- wait hogs javascript for 3 seconds preventing other things like opening window from happening
- you close each window immediately after opening it ?
Welcome to the forum
My real problem is logging to a single popup window. My log program works fine on Netscape 4.79 and in IE6 but won't work in Firefox. By "works fine" I mean log messages are displayed in real time as they happen rather than all at the end as for Firefox.
The wait(3000) simulates the time taken for various other programming tasks which come between log messages.
You say "wait hogs javascript for 3 seconds preventing other things like opening window from happening" but really the line w1.document.write("First Window<BR>") comes before the wait(3000) so you would expect the text "First Window" to appear before too. Indeed it does for IE6 but not Firefox. Maybe Firefox is not obeying commands in the right order. Maybe it treats window operations differently from normal operations, I don't know but whatever it is I have a big headache since the log is no good at all if messages are not in real time.
So the problem is better shown like this:-
function test() {
doWindow('first log message')
wait(3000)
doWindow('second log message')
}
function doWindow(txt) {
if(typeof w1=="undefined" ¦¦ !w1 ¦¦ w1.closed) {
w1 = window.open("", 'logwin')
}
w1.document.write(txt + "<BR>");
}
Try it in IE and then in Firefox to see the difference. IE6 (probably IE7 too) displays the text "First Window" well before the text "Second Window" appears. With Firefox all text displays together after 3 seconds.
I tried setTimeout:-
function test() {
setTimeout("doWindow('first log message')", 1)
wait(3000)
setTimeout("doWindow('second log message')", 1)
}
but it doesn't work. All messages display at the end for both Firefox and IE. I am beginning to think there is no solution.
Thanks very much for your help.
but really the line w1.document.write("First Window<BR>") comes before the wait(3000)
Ideally you are correct, but the new window I think has its own javascript tread running asynch to main thread. For whatever reason as you say things happen out of order.
but the wait(3000) is crucial and has been removed.
this line says wait 3 secs then do doWindow(2)
window.setTimeout("doWindow(2)", 3000);
Don't understand why this is not a suitable substitute, unless you want the 3 seconds delay to occur after window 1 closed or something else ?
Unfortunately my example is poor because the 3 second wait can be added to the setTimeout as you have done to get a solution.
Please have a look at this example which does not use a simple delay as before. I don't think the setTimeout method can be used here:-
function test() {
doWindow('first log message')
slowFunc(17, 600)
doWindow('2nd log message')
slowFunc(17, 600)
doWindow('end of log messages')
}
function doWindow(txt) {
if(typeof w1=="undefined" ¦¦ !w1 ¦¦ w1.closed) {
w1 = window.open("", 'logwin')
}
w1.document.write(txt + "<BR>");
}
function slowFunc(stringSize, numloops) {
var str="absdefg", i, ix
// Make a large string with "1234" in the middle.
for(i=0; i<stringSize; ++i) {
str += str
}
str = str + "1234" + str
// Find "1234" repeatedly
for(i=0; i<numloops; ++i) {
ix = str.indexOf("1234")
}
doWindow('slowFunc done')
}
If you try it in IE and Firefox you can see the different behaviour. In IE the log messages come out one by one in as they happen but in Firefox they all come at the end. What I need is some way for Firefox to display the log messages in real-time too.