Why this does not work?
-
Hi, Can u plz tell me that why this script doesn't work if I uncomment the commented line. JavaScript Test <!-- function clock() { var today=new Date(); var hr=today.getHours(); var min=today.getMinutes(); var sec=today.getSeconds(); if(hr < 10) hr=" "+hr; if(min < 10) min="0"+min; if(sec < 10) sec="0"+sec; var el=document.getElementById(1); el.innerHTML=hr+" : "+min+" : "+sec; document.clock.digits.value = hr + " : " + min + " : " + sec ; //document.write(hr+" : "+min+" : "+sec); setTimeout("clock()",1000); } //-->
-
Hi, Can u plz tell me that why this script doesn't work if I uncomment the commented line. JavaScript Test <!-- function clock() { var today=new Date(); var hr=today.getHours(); var min=today.getMinutes(); var sec=today.getSeconds(); if(hr < 10) hr=" "+hr; if(min < 10) min="0"+min; if(sec < 10) sec="0"+sec; var el=document.getElementById(1); el.innerHTML=hr+" : "+min+" : "+sec; document.clock.digits.value = hr + " : " + min + " : " + sec ; //document.write(hr+" : "+min+" : "+sec); setTimeout("clock()",1000); } //-->
Naturally
document.write(...);
is placed in blocks inside the <body>. As the browser loads the html document if it finds a <code>document.write(...); code it appends the dynamically generated content and continue loading the rest of the document. What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. So it just replace the current document with the new one. (you assume it sould append it to the end, right? ;P) In IE the next fire of timer can't find the clock() function and gives you a jscript error. In mozilla the timer is clear along with the document. Try removing the onload event and put a clock() before . Now the first call to clock() does what you wanted, but the first fire of timer replaces the document since the caller (timer event that is..) can not be located as a place in the loaded document. you might call clock() within a loop but this also fire a 'script taking to long' box. I also try document.write("time is:"+hr+" : "+min+" : "+sec+""+clock+""); but by the time document.write is called, everything else is lost. - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234 -
Naturally
document.write(...);
is placed in blocks inside the <body>. As the browser loads the html document if it finds a <code>document.write(...); code it appends the dynamically generated content and continue loading the rest of the document. What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. So it just replace the current document with the new one. (you assume it sould append it to the end, right? ;P) In IE the next fire of timer can't find the clock() function and gives you a jscript error. In mozilla the timer is clear along with the document. Try removing the onload event and put a clock() before . Now the first call to clock() does what you wanted, but the first fire of timer replaces the document since the caller (timer event that is..) can not be located as a place in the loaded document. you might call clock() within a loop but this also fire a 'script taking to long' box. I also try document.write("time is:"+hr+" : "+min+" : "+sec+""+clock+""); but by the time document.write is called, everything else is lost. - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234You said: What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. Then why it does not happen with div and form element. As I am new to javascript I require more clear explanation. Thanks Ranjan
-
You said: What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. Then why it does not happen with div and form element. As I am new to javascript I require more clear explanation. Thanks Ranjan
The write() function was intend to create dynamic content during the document load.
line1 document.write('hello world!') line 3
This is what you see:line1
hello world!
line3As you can see the code is executed and the result is placed right when it is encounter. If the write() function is called due to an event then where should it place it's result? - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234
-
The write() function was intend to create dynamic content during the document load.
line1 document.write('hello world!') line 3
This is what you see:line1
hello world!
line3As you can see the code is executed and the result is placed right when it is encounter. If the write() function is called due to an event then where should it place it's result? - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234