Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: how to generate a html code including javascript with javascript


Message #1 by "Hendra Haliman" <halimanh@i...> on Sun, 17 Nov 2002 22:15:15
> let say I want to generate this using a javascript

> <html>
 >  <head>
 >    <title>generated</title>
 >    <script language="javascript" type="text/javascript">
 >      function test(){
 >        alert('just a test');
 >      };
 >    </script>
 >  </head>
 >  <body onLoad=javascipt:test()>Hello world
 >  </body>
<> /html>

> with something like this...

> <html>
<> head>
 >  <title>javascript test</title>
 >  <script language="javascript" type="text/javascript">
 >    function generate(){ 
 >      newDoc=window.open('','newWin','toolbar=yes,location=yes');
 >      newDoc.document.write('<html>');
 >      newDoc.document.write('  <head>');
 >      newDoc.document.write('    <title>generated</title>');
 >      newDoc.document.write('    <script language="javascript" 
t> ype="text/javascript">');
 >      newDoc.document.write('      function test(){');
 >      newDoc.document.write("        alert('just a test');");
 >      newDoc.document.write('      };');
 >      newDoc.document.write('    </script>');
 >      newDoc.document.write('  </head>'); 
 >      newDoc.document.write('  <body onLoad=javascipt:test()>Hello 
world');
 >      newDoc.document.write('  </body>');
 >      newDoc.document.write('</html>');
 >    };
 >   </script>
<> /head>
<> body onLoad=javascript:generate()>I'm testing generating an html with 
j> avascript
<> /body>
<> html>

> but it did not work. Any ideas how ?

Slightly off the topic but it may give you another idea.
I am generating HTML in my CGI/perl script. Instead of generating all the 
JavaScript code on the fly I put it all in an external .js file which I 
refer to in the CGI code i.e. '<SCRIPT SRC="/js/file.js@></SCRIPT>'
In order to customize the code I provide some variables at the top of the 
HTML, in the HEAD section.
The code in file.js use these variables.

So, maybe you could put most of your code in a .js file and just create 
the instance-specific variables with document.write.

  Return to Index