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

Javascript document.write question

P: n/a
Hello

I have a block of html code that I want to run by calling a javascript
function to print it. Its basically a table with menu items in it that is
the same for many pages, and instead of copying/pasting everytime I change
it, I figure this will be better, as I only change it once.

The problem is, document.write doesn't handle multiple lines very well, so I
was wondering what is the best way to do this? Maybe there is even a better
way then javascript calls.

Anything will be great! Thanks!

Brett
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Brett Baisley wrote:
Hello

I have a block of html code that I want to run by calling a javascript
function to print it. Its basically a table with menu items in it that is
the same for many pages, and instead of copying/pasting everytime I change
it, I figure this will be better, as I only change it once.

The problem is, document.write doesn't handle multiple lines very well, so I
was wondering what is the best way to do this? Maybe there is even a better
way then javascript calls.


What do you mean 'doesn't handle multiple lines very well'? If you mean
the data is not easy to read on the generated page, insert \n where you
want a newline to begin. Post your code for more specific suggestions.

http://www.devguru.com/Technologies/...haracters.html

This code generates a new window with a table.

<html>
<title>CodeAve.com(JavaScript New Window within Previous Page)</title>
<body bgcolor="#FFFFFF">
<script language="JavaScript">
<!--
function open_new_window() {
new_window =
open("","displayWindow","width=575,height=620,left =10,top=10\n");

// open new document
new_window.document.open();

// Text of the new document goes here
// Replace your " with ' or \" or document.write statements will fail

new_window.document.write("<html>\n");
new_window.document.write("<head>\n");
new_window.document.write("<meta http-equiv='Content-Type'
content='text/html; charset=windows-1252'>\n");
new_window.document.write("<title>Table of Data</title>\n");
new_window.document.write("<style type='text/css'>\n");
new_window.document.write(" td { text-align: right; }\n");
new_window.document.write(" td.1 { text-align: left; }\n");
new_window.document.write("</style>\n");
new_window.document.write("</head>\n");
new_window.document.write("<body>\n");
new_window.document.write("<table border='1' cellspacing='1' width='555'
height='546' style='border-collapse: collapse'>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td class = '1' width='111'>Sonnenstand
in Grad über dem Horizont ein<br>Elevation of the sun (degrees above the
horizon</td>\n");
new_window.document.write(" <td class = '1' width='111'>Optimaler
Anstellwinkel des Spiegels °A<br>Optimal mirror orientation °A</td>\n");
new_window.document.write(" <td class = '1'
width='111'>Energieerzeugung Watts<br>Power output Watts</td>\n");
new_window.document.write(" <td class = '1' width='111'>Mit der
erzeugten Energie können Sie Grams<br>With the above displayed energy
you can heat Grams</td>\n");
new_window.document.write(" <td class = '1' width='111'>converted to
lbs:</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>0°</td>\n");
new_window.document.write(" <td>60°</td>\n");
new_window.document.write(" <td>138,56 W</td>\n");
new_window.document.write(" <td>552 g</td>\n");
new_window.document.write(" <td>1 lb 3.5 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>5°</td>\n");
new_window.document.write(" <td>63°</td>\n");
new_window.document.write(" <td>150,12 W</td>\n");
new_window.document.write(" <td>598 g</td>\n");
new_window.document.write(" <td>1 lb 5.1 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>10°</td>\n");
new_window.document.write(" <td>67°</td>\n");
new_window.document.write(" <td>161,46 W</td>\n");
new_window.document.write(" <td>643 g</td>\n");
new_window.document.write(" <td>1 lb 6.7 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>15°</td>\n");
new_window.document.write(" <td>70°</td>\n");
new_window.document.write(" <td>172,48 W</td>\n");
new_window.document.write(" <td>687 g</td>\n");
new_window.document.write(" <td>1 lb 8.2 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>20°</td>\n");
new_window.document.write(" <td>73°</td>\n");
new_window.document.write(" <td>183,06 W</td>\n");
new_window.document.write(" <td>729 g</td>\n");
new_window.document.write(" <td>1 lb 9.7 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>25°</td>\n");
new_window.document.write(" <td>77°</td>\n");
new_window.document.write(" <td>193,13 W</td>\n");
new_window.document.write(" <td>769 g</td>\n");
new_window.document.write(" <td>1 lb 11.1 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>30°</td>\n");
new_window.document.write(" <td>80°</td>\n");
new_window.document.write(" <td>202,57 W</td>\n");
new_window.document.write(" <td>807 g</td>\n");
new_window.document.write(" <td>1 lb 12.5 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>35°</td>\n");
new_window.document.write(" <td>83°</td>\n");
new_window.document.write(" <td>211,30 W</td>\n");
new_window.document.write(" <td>842 g</td>\n");
new_window.document.write(" <td>1 lb 13.7 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>40°</td>\n");
new_window.document.write(" <td>87°</td>\n");
new_window.document.write(" <td>219,23 W</td>\n");
new_window.document.write(" <td>873 g</td>\n");
new_window.document.write(" <td>1 lb 14.8 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>45°</td>\n");
new_window.document.write(" <td>90°</td>\n");
new_window.document.write(" <td>226,27 W</td>\n");
new_window.document.write(" <td>901 g</td>\n");
new_window.document.write(" <td>1 lb 15.8 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>50°</td>\n");
new_window.document.write(" <td>93°</td>\n");
new_window.document.write(" <td>232,37 W</td>\n");
new_window.document.write(" <td>926 g</td>\n");
new_window.document.write(" <td>2 lbs 0.7 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>55°</td>\n");
new_window.document.write(" <td>97°</td>\n");
new_window.document.write(" <td>237,43 W</td>\n");
new_window.document.write(" <td>946 g</td>\n");
new_window.document.write(" <td>2 lbs 1.4 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>60°</td>\n");
new_window.document.write(" <td>100°</td>\n");
new_window.document.write(" <td>241,41 W</td>\n");
new_window.document.write(" <td>962 g</td>\n");
new_window.document.write(" <td>2 lbs 1.9 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>65°</td>\n");
new_window.document.write(" <td>103°</td>\n");
new_window.document.write(" <td>244,25 W</td>\n");
new_window.document.write(" <td>973 g</td>\n");
new_window.document.write(" <td>2 lbs 2.3 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>70°</td>\n");
new_window.document.write(" <td>107°</td>\n");
new_window.document.write(" <td>245,90 W</td>\n");
new_window.document.write(" <td>980 g</td>\n");
new_window.document.write(" <td>2 lbs 2.6 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>75°</td>\n");
new_window.document.write(" <td>110°</td>\n");
new_window.document.write(" <td>256,32 W</td>\n");
new_window.document.write(" <td>981 g</td>\n");
new_window.document.write(" <td>2 lbs 2.6 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>80°</td>\n");
new_window.document.write(" <td>113°</td>\n");
new_window.document.write(" <td>245,49 W</td>\n");
new_window.document.write(" <td>978 g</td>\n");
new_window.document.write(" <td>2 lbs 2.5 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>85°</td>\n");
new_window.document.write(" <td>117°</td>\n");
new_window.document.write(" <td>243,39 W</td>\n");
new_window.document.write(" <td>970 g</td>\n");
new_window.document.write(" <td>2 lbs 2.2 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write(" <tr>\n");
new_window.document.write(" <td>90°</td>\n");
new_window.document.write(" <td>120°</td>\n");
new_window.document.write(" <td>240,00 W</td>\n");
new_window.document.write(" <td>956 g</td>\n");
new_window.document.write(" <td>2 lbs 1.7 ozs</td>\n");
new_window.document.write(" </tr>\n");
new_window.document.write("</table>\n");
new_window.document.write("</body>\n");
new_window.document.write("</html>\n");

// close the document
new_window.document.close();
}
// -->
</script><a onclick="open_new_window()"
style="cursor:pointer;cursor:hand;" ><font color="#008080"><u>Open New
Window</u></font></a>

</body>
</html>

Jul 20 '05 #2

P: n/a
mscir wrote:
[...] If you mean the data is not easy to read on the generated page,
insert \n where you want a newline to begin. Post your code for more
specific suggestions.

http://www.devguru.com/Technologies/...haracters.html

This code generates a new window with a table.

<html>
DOCTYPE declaration?
<title>CodeAve.com(JavaScript New Window within Previous Page)</title>
<body bgcolor="#FFFFFF">
Text color?
<script language="JavaScript">
"type" attribute?
<!--
function open_new_window() {
new_window =
open("","displayWindow","width=575,height=620,left =10,top=10\n");
Wrong. The options (third) argument of window.open() must not
contain newline. And _window._open() is recommended since there
is document.open(). The function should be defined within
the "head" element, not the "body" element, to be available always.
// open new document
new_window.document.open();

// Text of the new document goes here
// Replace your " with ' or \" or document.write statements will fail

new_window.document.write("<html>\n");
See above.
new_window.document.write("<head>\n");
new_window.document.write("<meta http-equiv='Content-Type'
content='text/html; charset=windows-1252'>\n"); ^^^^^^^^^^^^
What about UAs that does not support this Windows character set?
ISO-8859-1 is better.
new_window.document.write("<title>Table of Data</title>\n");

^^
Not only that this is and creates invalid HTML for the most part, a
smaller example would have sufficed. You have posted more than 200
lines of garbage!
PointedEars
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.