Recently I have been doing some Jscripting and whenever i do a document.writeln it gets all wierd on me:?:.
look a this code for example:
Function write {
if (document.mytext == null) {
document.writeln("There used to be no text here.");
}
}
if i hooked this up to a onload function, the page would only display what i said to write in, and everything else in the document would dissapear.
What do i do???

Recommended Answers

All 18 Replies

The various document.write methods are depcrecated, and will no longer work in the newer HTML doctypes.

One thing that many forget to do when using document.write(), is to close the document! Be sure you use a document.close() statement at the appropriate spot.

Please elaborate on your question... I don't know which part of my response you don't understand.

why and how do you close the document?

document.close();

That's the "how", as to "why", you have to let the browser know you are done re-writing the document, so it can render it. If you don't do a "document.close()", you'll get varying, unexpected results from one browser to another.

Where do I place document.close(); inside my document??

After your final "document.write()" statement, inside your "Write" function.

what is the "Write" function??

I think he means if you have a specific function that is doing all the writes. Basically, close the document when you are done writing to it.

YOU created a function called "write" in the code you posted. Add a document.close() statement to it.

function write()
{
  if (document.mytext == null)
  {
	document.writeln("There used to be no text here.");
	document.close();
  }
}

I also posted corrections to the code and formatted it in a style I use to align brackets and make the code easier to maintain.

Hi,even I have a similsr kind of problem with document.writeln().When Im using the statement the nullpage is being shown in mozilla browser but its running fine in Internet Explorer.Can you please help me out.
I also checked it out by using document.close() which was of no use to me

If you want to add text to a document use the DOM to create a text node and append it.

var textNode = document.createTextNode("Some text");
document.getElementsByTagName("body")[0].appendChild(textNode);

Can U just give me the reason why a documenty.writeln() statement returns a null page in mozilla but works properly in Internet Explorer

document.writeln('<img name="imgPageDot" style="visibility:hidden" border="0" src="' + sImgLocation + '" alt="" />');
here the variable sImgLocation contains the image src..

Please do reply to my question!! Im in need of the reply

When you say 'null page' what is that? a blank white page? or does Mozilla have a specific 'null' error page?

Your code (the bit you have posted) writes a hidden image so I wouldn't expect to see anything in any browser anyway. Can you post full code *IN CODE TAGS* please.

Also document.writeln is really for adding text not elements (In my opinion I may be mistaken), you should use DOM to add elements using the same technique for text that I've already posted as it's more likey to work in many browsers.

If you document.write( ) AFTER the page has loaded fully, it tends to write in a new, blank page ( http://javascript.about.com/library/blwrite.htm ). It's not particularly well standardized behaviour between the browsers, so you're advised not to use document.write( ) after the page has been loaded ( i.e. button click -> document.write or similar is a no-go ). I guess, 'why' it doesn't work is because only with a document.write executed as the page loads can the browser infer in any reasonable way 'where' to put the written content. After the page has loaded, there is no reference point as to where the written content is supposed to go. IE assumes that's it's after the rest of the content in the body element, but the behaviour of document.write isn't defined after the loading phase, so any browser can, reasonably, do what they want with the content. Firefox puts it in a new window.

Infact, you're not even supposed to use it while the page is loading if your working with XHTML ( http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite ).

Thank you for giving the reason[:)] .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.