Results 1 to 3 of 3

Thread: Document.write other

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Document.write other

    Is there a javascript function like echo in php to put the contents of a javascript value? I can't use the document.write because it is being called in a function (function's not the correct term above, what are those called?) that loads multiple times.

    I'm trying to output this full array if that makes a different.
    a = a + 1;
    list_of_files[a] = file.name;
    I figured putting whatever outputs the result in a while loop like this would display all the contents or is there an output array contents function (function phrased used wrong again).

    while (a < 0 ) {
    codetowrite(list_of_files[a]);
    a=a-1;
    }

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    It's called a while statement. Make an array called data, and in the while statement, add the information to string within that array.
    Good luck!
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The document.write() method is rather frowned upon these days. However, with judicious and limited use, it can still be quite effective in certain cases.

    It's two major drawbacks are:

    1. If it is executed after page load, it overwrites the page.
    2. It encourages creating markup from strings, which is less portable and often less effective than markup created using the DOM.


    For simple one-liners though, it can be OK. You can (though it is not recommended) even use it to write out a concatenation produced from iterating over an array of strings (as I believe you are proposing), as long as the write phase occurs while the page is being parsed, at the point of parse. Otherwise, the overwrite disadvantage will kick in.

    The original question here seems to me to have two parts though:

    1. How do I concatenate an array of strings?
    2. How do I inject this concatenated string into my document?


    There are at least a few answers to both questions. Here is a demo showing one way:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    var ar = ['string1', 'string2', 'string3'];
    onload = function(){
    	document.getElementById('targetArea').firstChild.nodeValue = ar.join(' ');
    };
    </script>
    </head>
    <body>
    <div id="targetArea">&nbsp;</div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bluewalrus (11-03-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •