Javascript document.write question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Baisley

    Javascript document.write question

    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


  • mscir

    #2
    Re: Javascript document.write question

    Brett Baisley wrote:[color=blue]
    > 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.[/color]

    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.



    This code generates a new window with a table.

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

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

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

    new_window.docu ment.write("<ht ml>\n");
    new_window.docu ment.write("<he ad>\n");
    new_window.docu ment.write("<me ta http-equiv='Content-Type'
    content='text/html; charset=windows-1252'>\n");
    new_window.docu ment.write("<ti tle>Table of Data</title>\n");
    new_window.docu ment.write("<st yle type='text/css'>\n");
    new_window.docu ment.write(" td { text-align: right; }\n");
    new_window.docu ment.write(" td.1 { text-align: left; }\n");
    new_window.docu ment.write("</style>\n");
    new_window.docu ment.write("</head>\n");
    new_window.docu ment.write("<bo dy>\n");
    new_window.docu ment.write("<ta ble border='1' cellspacing='1' width='555'
    height='546' style='border-collapse: collapse'>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td class = '1' width='111'>Son nenstand
    in Grad über dem Horizont ein<br>Elevatio n of the sun (degrees above the
    horizon</td>\n");
    new_window.docu ment.write(" <td class = '1' width='111'>Opt imaler
    Anstellwinkel des Spiegels °A<br>Optimal mirror orientation °A</td>\n");
    new_window.docu ment.write(" <td class = '1'
    width='111'>Ene rgieerzeugung Watts<br>Power output Watts</td>\n");
    new_window.docu ment.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.docu ment.write(" <td class = '1' width='111'>con verted to
    lbs:</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>0°</td>\n");
    new_window.docu ment.write(" <td>60°</td>\n");
    new_window.docu ment.write(" <td>138,56 W</td>\n");
    new_window.docu ment.write(" <td>552 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 3.5 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>5°</td>\n");
    new_window.docu ment.write(" <td>63°</td>\n");
    new_window.docu ment.write(" <td>150,12 W</td>\n");
    new_window.docu ment.write(" <td>598 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 5.1 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>10°</td>\n");
    new_window.docu ment.write(" <td>67°</td>\n");
    new_window.docu ment.write(" <td>161,46 W</td>\n");
    new_window.docu ment.write(" <td>643 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 6.7 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>15°</td>\n");
    new_window.docu ment.write(" <td>70°</td>\n");
    new_window.docu ment.write(" <td>172,48 W</td>\n");
    new_window.docu ment.write(" <td>687 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 8.2 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>20°</td>\n");
    new_window.docu ment.write(" <td>73°</td>\n");
    new_window.docu ment.write(" <td>183,06 W</td>\n");
    new_window.docu ment.write(" <td>729 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 9.7 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>25°</td>\n");
    new_window.docu ment.write(" <td>77°</td>\n");
    new_window.docu ment.write(" <td>193,13 W</td>\n");
    new_window.docu ment.write(" <td>769 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 11.1 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>30°</td>\n");
    new_window.docu ment.write(" <td>80°</td>\n");
    new_window.docu ment.write(" <td>202,57 W</td>\n");
    new_window.docu ment.write(" <td>807 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 12.5 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>35°</td>\n");
    new_window.docu ment.write(" <td>83°</td>\n");
    new_window.docu ment.write(" <td>211,30 W</td>\n");
    new_window.docu ment.write(" <td>842 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 13.7 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>40°</td>\n");
    new_window.docu ment.write(" <td>87°</td>\n");
    new_window.docu ment.write(" <td>219,23 W</td>\n");
    new_window.docu ment.write(" <td>873 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 14.8 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>45°</td>\n");
    new_window.docu ment.write(" <td>90°</td>\n");
    new_window.docu ment.write(" <td>226,27 W</td>\n");
    new_window.docu ment.write(" <td>901 g</td>\n");
    new_window.docu ment.write(" <td>1 lb 15.8 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>50°</td>\n");
    new_window.docu ment.write(" <td>93°</td>\n");
    new_window.docu ment.write(" <td>232,37 W</td>\n");
    new_window.docu ment.write(" <td>926 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 0.7 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>55°</td>\n");
    new_window.docu ment.write(" <td>97°</td>\n");
    new_window.docu ment.write(" <td>237,43 W</td>\n");
    new_window.docu ment.write(" <td>946 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 1.4 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>60°</td>\n");
    new_window.docu ment.write(" <td>100°</td>\n");
    new_window.docu ment.write(" <td>241,41 W</td>\n");
    new_window.docu ment.write(" <td>962 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 1.9 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>65°</td>\n");
    new_window.docu ment.write(" <td>103°</td>\n");
    new_window.docu ment.write(" <td>244,25 W</td>\n");
    new_window.docu ment.write(" <td>973 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 2.3 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>70°</td>\n");
    new_window.docu ment.write(" <td>107°</td>\n");
    new_window.docu ment.write(" <td>245,90 W</td>\n");
    new_window.docu ment.write(" <td>980 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 2.6 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>75°</td>\n");
    new_window.docu ment.write(" <td>110°</td>\n");
    new_window.docu ment.write(" <td>256,32 W</td>\n");
    new_window.docu ment.write(" <td>981 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 2.6 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>80°</td>\n");
    new_window.docu ment.write(" <td>113°</td>\n");
    new_window.docu ment.write(" <td>245,49 W</td>\n");
    new_window.docu ment.write(" <td>978 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 2.5 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>85°</td>\n");
    new_window.docu ment.write(" <td>117°</td>\n");
    new_window.docu ment.write(" <td>243,39 W</td>\n");
    new_window.docu ment.write(" <td>970 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 2.2 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write(" <tr>\n");
    new_window.docu ment.write(" <td>90°</td>\n");
    new_window.docu ment.write(" <td>120°</td>\n");
    new_window.docu ment.write(" <td>240,00 W</td>\n");
    new_window.docu ment.write(" <td>956 g</td>\n");
    new_window.docu ment.write(" <td>2 lbs 1.7 ozs</td>\n");
    new_window.docu ment.write(" </tr>\n");
    new_window.docu ment.write("</table>\n");
    new_window.docu ment.write("</body>\n");
    new_window.docu ment.write("</html>\n");

    // close the document
    new_window.docu ment.close();
    }
    // -->
    </script><a onclick="open_n ew_window()"
    style="cursor:p ointer;cursor:h and;" ><font color="#008080" ><u>Open New
    Window</u></font></a>

    </body>
    </html>

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Javascript document.write question

      mscir wrote:
      [color=blue]
      > [...] 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>[/color]

      DOCTYPE declaration?
      [color=blue]
      > <title>CodeAve. com(JavaScript New Window within Previous Page)</title>
      > <body bgcolor="#FFFFF F">[/color]

      Text color?
      [color=blue]
      > <script language="JavaS cript">[/color]

      "type" attribute?
      [color=blue]
      > <!--
      > function open_new_window () {
      > new_window =
      > open("","displa yWindow","width =575,height=620 ,left=10,top=10 \n");[/color]

      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.
      [color=blue]
      > // open new document
      > new_window.docu ment.open();
      >
      > // Text of the new document goes here
      > // Replace your " with ' or \" or document.write statements will fail
      >
      > new_window.docu ment.write("<ht ml>\n");[/color]

      See above.
      [color=blue]
      > new_window.docu ment.write("<he ad>\n");
      > new_window.docu ment.write("<me ta http-equiv='Content-Type'
      > content='text/html; charset=windows-1252'>\n");[/color]
      ^^^^^^^^^^^^
      What about UAs that does not support this Windows character set?
      ISO-8859-1 is better.
      [color=blue]
      > new_window.docu ment.write("<ti tle>Table of Data</title>\n");[/color]
      ^^
      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

      Comment

      Working...