View Full Version : Resolved document.write?

06-16-2009, 08:05 AM
why document.write takes me to another page?
i want to write in the original not another page? what should i do?


<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page</title>
function click(){
document.write('<div style="position: absolute; width: 47px; height: 47px; z-index: 1; left: 132px; top: 66px" id="layer1"> <img border="0" src="file:///H:/Documents%20and%20Settings/Administrator/Desktop/ajax-loader.gif" width="48" height="47"></div>');
return false;


<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center"><a onclick="return click()" href="javascript:void(0)">link</a></p>



Philip M
06-16-2009, 08:23 AM
document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

Your best plan is to load the <div> at the outset and use style="display:none", revealing the <div> onclick.

Jesus saves, Moses invests, Allah protects, and Cthulhu thinks you'd make a nice sandwich.

06-16-2009, 10:15 AM
should i use DOM objects to create elements after loading? is it possible?
thank you.

06-16-2009, 10:22 AM
You may either change the Object.style.display (http://www.w3schools.com/htmldom/prop_style_display.asp) as Philip mentioned or use createElement() and appendChild methods to add new elements.

06-16-2009, 04:07 PM
createElement is probably best, but you can also give a <div> an id (<div id="mydiv")>) and then change the innerHTML property up in the onclick handler function:

document.getElementById('mydiv').innerHTML='some text here, some <div style="font-size:50px;">CSS & HTML</div>, <img src="myimg.jpg">, etc.'

06-17-2009, 04:09 PM
thank you all.