...

View Full Version : Resolved Need alternate to the "documnt.write" line.



MancunianMacca
03-01-2012, 10:54 PM
Hey guys, I need help... I want to have this script



<script type = "text/javascript">
function runMyJavascript_Caught()
{
// get user to add name
var name_entry = prompt ("Please Enter your Name!", "James");

// declare some short strings

var stating = ", I knew you did it!";
var told = "I told you that I would find out";
var mistake = "I guess you just made a mistake entering your name, ";
var punish = "Now go back to your room";
var heading = "OI Stop";

// construct some longer strings

var stating_name = name_entry + stating;
var mistake_name = mistake + name_entry;

document.writeln(heading.bold() + "<br>");
document.writeln(stating_name + "<br>");
document.writeln(told + "<br>");
document.writeln(mistake_name + "<br>");
document.writeln(punish + "<br>");
}
</script>

write on a button command. What can I use insted of document.write?

Old Pedant
03-01-2012, 11:32 PM
Time (past time!) to learn about the DOM.



var element = document.getElementById("idOfSomeElementOnThePage");
element.innerHTML ="<b>" + heading + "</b><br>" + stating_name + "<br>"
+ told + "<br>" + mistake_name + "<br>" + punish + "<br>";

But there are other and, some say, better ways.

webdev1958
03-02-2012, 05:45 AM
What can I use insted of document.write?

createElement() and createTextNode() are DOM methods you could use instead of document.write().

felgall
03-02-2012, 08:36 AM
innerHTML works fine as long as you don't subsequently need to reference any of the added elements from JavaScript and as long as you don't try to add part of a table. Either of those will not work in all browsers.

It will work for anything that used to use document.write and was the quickest way of disposing of all the document.write statements after Netscape 4 dies in 2005. It made the complete disappearance of the document.write statement by early 2006 possible (ore would have if people actually bothered to update prehistoric scripts rather than continuing to create more).

Anyone learning JavaScript in recent years should be spending about 1/3 of the course learning all the different DOM commands as that is now one of the largest and certainly the most useful part of JavaScript.

MancunianMacca
03-02-2012, 06:14 PM
innerHTML works fine as long as you don't subsequently need to reference any of the added elements from JavaScript and as long as you don't try to add part of a table. Either of those will not work in all browsers.

It will work for anything that used to use document.write and was the quickest way of disposing of all the document.write statements after Netscape 4 dies in 2005. It made the complete disappearance of the document.write statement by early 2006 possible (ore would have if people actually bothered to update prehistoric scripts rather than continuing to create more).

Anyone learning JavaScript in recent years should be spending about 1/3 of the course learning all the different DOM commands as that is now one of the largest and certainly the most useful part of JavaScript.

Thanks for the help guys I resolved this :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum