SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    correct JavaScript

    What is the correct JavaScript syntax to print "Hello World" on the page?
    a. response.write(‘Hello World’);
    b. document.write(‘Hello World’);
    c. document.print(‘Hello World’);
    d. document.write = ‘Hello World’;

  2. #2
    .•*º draziW tnioPetiS º*•. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For HTML: b.
    For XHTML: none of the above.
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    e.
    var s=document.createTextNode('Hello World');
    document.getElementsByTagName('body')[0].appendChild(s);

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    f.
    Code:
    var p = document.createElement("p");
    p.appendChild(document.createTextNode("Hello, World"));
    document.getElementsByTagName("body")[0].appendChild(p);
    Birnam wood is come to Dunsinane

  6. #6
    .•*º draziW tnioPetiS º*•. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo
    e.
    var s=document.createTextNode('Hello World');
    document.getElementsByTagName('body')[0].appendChild(s);
    Nice! My fav so far.

    Conclusion:

    1. Traditional: document.write("Hello World");
    2. If XHTML: None of the above.
    3. Contemporary: var s=document.createTextNode('Hello World'); document.getElementsByTagName('body')[0].appendChild(s);
    Sound/look (document.)right?

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For XHTML:
    Code:
    var p = document.createElementNS("http://www.w3.org/1999/xhtml", "p");
    p.appendChild(document.createTextNode("Hello, World"));
    document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "body")[0].appendChild(p);
    An alternate version of the last line (for both HTML and XHTML) could be,
    Code:
    document.documentElement.lastChild.appendChild(p);
    (Provided there's no whitespace text node after the </html> tag.)
    Birnam wood is come to Dunsinane

  8. #8
    .•*º draziW tnioPetiS º*•. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very cool! Hehe, it is amazing how something so basic can be so interesting... well, at least to me it is... and when compared to the more traditional document.write(""); syntax.

  9. #9
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    g. jQuery:
    Code:
    $('<p>Hello World!</p>').appendTo('body');

  10. #10
    .•*º draziW tnioPetiS º*•. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Niiiice! Lol!

    As always, "Hello World" rocks!

  11. #11
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for no. of scraps


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
  •