www.webdeveloper.com
Results 1 to 12 of 12

Thread: how to write newline, and text in javascript?

  1. #1
    Join Date
    Nov 2005
    Location
    shores of Lake Ontario, Canada
    Posts
    57

    Question how to write newline, and text in javascript?

    I sure could use:
    document.write('<br>');
    or similar for text output, but this seems to create a new document, not append to the existing one. I don't fully understand why. I read it's because of some timing issue. In any case, I don't know how to display other stuff together with document.write()
    I currently use:
    Code:
    function makeButton()
    {	myParagraph = document.getElementById("paragraph1");
    
    	myButton = document.createElement("button");
    	buttonText1 = document.createTextNode("button In Form");
    	myButton.appendChild(buttonText1);
    	myButton.onclick = myOnClick;
    	myParagraph.appendChild(myButton);
    }
    ....
    <body>
    <p id="paragraph1">
    &nbsp;
    </p>
    etc.
    and am wondering what are convenient ways of writing text.
    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    USA
    Posts
    10
    using the create element is the prefered way to do it as most other methods are being phased out by W3C as far as I know. Also with document.write() you ahve .open() and .close. although you could just use .innerHTML = "but this is being phased out too i think";

  3. #3
    Join Date
    Nov 2005
    Location
    shores of Lake Ontario, Canada
    Posts
    57
    thanks, but what is the new line character/string, etc. in javascript?

  4. #4
    Join Date
    Apr 2005
    Posts
    387
    document.write() adds to the document before the onload event.

    for new line either
    someelement.innerHTML='text<br>';

    or

    var br=document.createElement('br');
    someelement.appendChild(br);

  5. #5
    Join Date
    Nov 2005
    Location
    shores of Lake Ontario, Canada
    Posts
    57
    Thanks, the latter method works, and after reading the answer by wetlander, I probably shouldn't even try the former method, :-)

    Now, how do I output text: static text and dynamic text, i.e. from a variable.
    I tried:
    Code:
    	myText = document.createElement('test');
    	myParagraph.appendChild(myText);
    but nothing shows up...

  6. #6
    Join Date
    Apr 2005
    Posts
    387
    myText = document.createTextNode('test');
    myParagraph.appendChild(myText);

  7. #7
    Join Date
    Apr 2005
    Posts
    387
    innerHTML is useful and in some case much less complicated. All the major browsers support it. W3C hasn't yet adopted it but I doubt the browsers will ever stop supporting it.

    It's like the whole html is dead thing. Who is going to be the first to build a browser that doesn't support html?
    Last edited by gph; 02-02-2006 at 10:43 PM.

  8. #8
    Join Date
    Nov 2005
    Location
    shores of Lake Ontario, Canada
    Posts
    57
    thanks, it works with dynamic text too. I'm almost ready to rewrite my html code with javascript.

  9. #9
    Join Date
    Apr 2005
    Posts
    387
    Depending on your situation that might not be a good approach. JavaScript disabled browsers and search engine bots won't see anything.

  10. #10
    Join Date
    Nov 2005
    Location
    shores of Lake Ontario, Canada
    Posts
    57
    yeah, I'm aware of potential problems... But the thing is I'm having a larger amount of repeatable code that requires dynamic changes. And there is a mini data base for which I need javascript. I don't think it can be done in html. Javascript is the only other web tool that I know (or rather am trying to learn).
    In the meantime, I'll try to have an alternative page with limited functionality (as nice as reasonable) for browsers not supporting/disabled javascript.

  11. #11
    Join Date
    Apr 2005
    Posts
    387
    I see, in case you're not aware.

    <noscript>js disabled html</noscript>

  12. #12
    Join Date
    Feb 2006
    Location
    USA
    Posts
    10
    i have run into problems with using innerHTML on firefox (very limited problems there). and bigger problems on safari. Usually works fine though, but doesn't really created the elements properly. For just throwing some text in, it's probably the best method at the moment due to lines of code.

    another problem with dynamic text other than people with js disabled....search engine spiders don't see it either.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles