SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member AverageMax's Avatar
    Join Date
    Jul 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nodeName Problem

    Hello,

    I'm reading Simply JavaScript and I ran into a problem with an example from a book. I can't get nodeName to work properly. Am I doing something wrong?

    Here's my code:

    HTML Code:
    <body>
      <h4 id="movieTitle">Analyze This</h4>
    
      <p>
        The film stars Robert De Niro as a mafioso and Billy Crystal as a psychiatrist.
      </p>
    </body>
    Code JavaScript:
    var target = document.getElementById("movieTitle");
     
    alert(target.nodeName);

    Help me, please. Thank you in advance.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Hi AverageMax, welcome to the forums,

    Just guessing. Is the javascript running before the page's DOM loads in?

  3. #3
    SitePoint Member AverageMax's Avatar
    Join Date
    Jul 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know. I'm just starting with JavaScript. How can I check that?

    Thank you.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Well, there are a few ways things can be done.

    If the javascript is in the <head> and isn't in a function that's called by onload (preferrably by adding an event listener to the body IMHO) or at the end of the page then the script runs before the page's DOM is loaded in so the tags won't be there yet to get. eg. this won't work:
    HTML Code:
    <html><head><title>nodeName test</title>
    <script type='text/javascript'>
    var target = document.getElementById("movieTitle");
    alert(target.nodeName);
    </script>
    </head>
    <body>
      <h4 id="movieTitle">Analyze This</h4>
      <p>
        The film stars Robert De Niro as a mafioso and Billy Crystal as a psychiatrist.
      </p>
    </body></html>
    but this will:
    HTML Code:
    <html><head><title>nodeName test</title>
    </head>
    <body>
      <h4 id="movieTitle">Analyze This</h4>
      <p>
        The film stars Robert De Niro as a mafioso and Billy Crystal as a psychiatrist.
      </p>
    <script type='text/javascript'>
    var target = document.getElementById("movieTitle");
    alert(target.nodeName);
    </script>
    </body></html>
    *a quick and dirty example, but hopefully it helps

  5. #5
    SitePoint Member AverageMax's Avatar
    Join Date
    Jul 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My JavaScript is in an external file, but this works!

    Thank you very much!

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    I agree, putting your script at the bottom is the best technique to speed up your web page and improve your ability to work with the DOM.

    The Simply Scripting book uses its own custom library called Core, which includes a method called .start()
    Pages 58 and 59 of the book cover that, in the section titled Unobtrusive Scripting in the Real World.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member AverageMax's Avatar
    Join Date
    Jul 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I must have missed it.

    Thanks.


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
  •