Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write many HTML lines 1

Status
Not open for further replies.

saimon65

Technical User
Jan 2, 2002
16
AU
Hi.
To write a HTML line, document.write() is used.
Is there any shortcut method that can be used to write many HTML lines with just one method instead of using the function on every line?

For example, in PERL, there is a way to flag the start of the HTML lines and end of the lines. I need to do this sort of thing in JavaScript.

thanks.
 
One way is to concatenate all the lines into a single string and write that one string:
Code:
sHTML = "<html>";
sHTML += "<head>";
sHTML += "</head>";
sHTML += "<body>";
sHTML += "</body>";
sHTML += "</html>\n";
document.write(sHTML);
Note that this would actually all come out on a single line too, since there are no line breaks. If you want, or need, line breaks, just add \n to the end of each line, like in the last one above.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
you could also cut down on some code like this:

Code:
with (document) {
    open();
    writeln('...');
    writeln('...');
    writeln('...');
    writeln('...');
    close();
}

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Watch out for line wrapping...
Code:
<script>
[green]// Extend the function object[/green]
Function.prototype.getComment=function(){
  return this.toString().substring(this.toString().indexOf("/*")+2,this.toString().indexOf("*/"));
}

[green]// Declare your multiple line string[/green]
function myLongString(){/*
  <html>
    <head>
    </head>
    <body>
    </body>
  </html>
*/}

[green]// Write it to the page[/green]
var mywin = window.open();
mywin.document.write(myLongString.getComment());

</script>

Adam

There's no place like 127.0.0.1
 
adam: that's certainly an interesting approach to the problem, and one that would never have occured to me. I'll have to try that one out.

It does not appear to allow inserting variables into the string of html. Any way you can modify it to do that?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
You should be able to do a replace once you have the string. I do this by passing a function into the second argument. I know this works in JScript 5.5+, but I don't know if it's cross-browser compatible.
Code:
<script>
[green]// Extend the function object[/green]
Function.prototype.getComment=function(){
  return this.toString().substring(this.toString().indexOf("/*")+2,this.toString().indexOf("*/"));
}
[green]// Extend the string object[/green]
String.prototype.parseVariables=function(){
  return this.replace(/\<\#([^\>]+)\>/gi, [red]function(m,m1){return window[m1]}[/red]);
}

[green]// Declare your variables[/green]
var bodyContent = "Hello World";

[green]// Declare your multiple line string, adding your variables in this format[/green]
myLongString = function(){/*
  <html>
    <head>
    </head>
    <body>
      <#bodyContent>
    </body>
  </html>
*/}

[green]// Parse the variables[/green]
var output = myLongString.getComment().parseVariables();

[green]// Write it to the page[/green]
var mywin = window.open();
mywin.document.write(output);

</script>

Adam

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
That's OK, I don't really care if it's cross-browser compatible. I do most of my development these days for an application that requires IE.

Looks good, thanks!


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top