Forum Moderators: open

Message Too Old, No Replies

document.write()ing a block of HTML

an easy way?

         

ga_ga

1:41 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



In perl, I'm used to being able to print out blocks of HTML conditionally something along the lines of:

if (condition)
{
print "Content-type: text/html\n\n";

print <<ENDHTML;

<HTML>
<HEAD>
<TITLE>Details Successfully Added</TITLE>
<META http-equiv="CONTENT-LANGUAGE" content="EN">
etc

ENDHTML

}
else
{
do something else;
}

Can I use something similar in Javascript?

Thanks for any help.

(clarification.. it's the javascript equivalent of perl's 'print <<' I'm stuck on, not the conditional)

korkus2000

3:59 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can but it is more tedious.

<script>
if (condition)
{
document.write("<HTML><HEAD><TITLE>Details Successfully Added</TITLE>");
document.write("<META http-equiv="CONTENT-LANGUAGE" content="EN">");
}
else
{
document.write("Test");
}
</script>

You won't have a true print since the client is building the page and not the server like with perl.

ga_ga

5:23 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



Ok, thanks Korkus :-)

Arthalion

10:09 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



Hrm...shouldn't that code be escaped? e.g.:

<script>
if (condition)
{
document.write('<HTML><HEAD><TITLE>Details Successfully Added<\/TITLE>');
document.write('<META http-equiv=\"CONTENT-LANGUAGE\" content=\"EN\">');
}
else
{
document.write("Test");
}
</script>

I've always escaped my JavaScript code writes assuming that it wouldn't work any other way. Will JS ignore the internal slashes and quotation marks, or is escaping required?

korkus2000

10:21 pm on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are correct. I needed to escape the quotes.