By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

JavaScript Problem with document.write()

P: 1
Hi I am sunny and a started working on JavaScript recently.
I am stuck up in one script.

The Script goes on like this

[HTML]<HTML>
<HEAD>
<Script Language = "JavaScript">
function call()
{
var str = prompt("Welcome Your name is", "")
document.write(str)
}
</Script>
</HEAD>
<Body bg color=gray>
<Form name="test">
<Input type="Button" name="b1" value="submit" onClick="call()">
</Form>
</Body>
</HTML>
[/HTML]
My Question is
Why does the background color goes off when the "document.write" cmd executes.


Please guide me thru.
Nov 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: 34
document.write will write to the object from within it is called, for example if it was inside a div it will write to that div erasin everythin inside the div.

so whats happening here is your calling document.write from within the head. I guess it is erasing everything in the document and writing your string to the empty document.
Nov 21 '07 #2

gits
Expert Mod 5K+
P: 5,390
yes ... thats the point ... you may use innerHtml or proper dom-node-handling for that purpose ... you should never use document.write when a page is loaded already ....

kind regards

ps: changed thread-title to better describe the problem ...
Nov 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.