SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple script not working why?

    Why will this not work? This is crazy.

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <SCRIPT type="text/javascript">
    
    
    var target = document.getElementById("berenger");
    alert(target.nodeName);
    
    
    
    </SCRIPT>
    </HEAD>
    
    <BODY>
    
    <h1>
    	Sniper
    </h1>
    <p> 
    In this Cinema masterpiece, <A id="berenger" HREF="">Tom Berenger </A> plays a us soldier workin in jungle.
    </p>
    
    
    
    </BODY>
    </HTML>

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't work because you are accessing an element before it has loaded.

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like pepjeria said, you are trying to access an element which has not loaded. Just put it in a function onload:

    Code:
    window.onload = function() {
      var target = document.getElementById("berenger");
      alert(target.nodeName);
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Come on guys, we should stop using the window.onload technique.
    Our Javascript Guru list features an excellent unobtrusive javascript about how frontloading from the head is a bad idea and takes you through some backloading techniques instead.

    That's where you place the script at the bottom of the body, so that it can immediately access the page elements.

    Code html4strict:
    <html>
    <head>
    </head>
    <body>
    ...
    <script>
    var target = document.getElementById("berenger");
    alert(target.nodeName);
    </script>
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That's where you place the script at the bottom of the body, so that it can immediately access the page elements.
    I'm just too lazy to scroll down to the </body>
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    In many cases the mouse is slower than the keyboard.

    Ctrl+End
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Come on guys, we should stop using the window.onload technique.
    There is nothing wrong with adding the functionality with window.onload. My opinion is that it is better to include JavaScript funtionality in an external file (which is cached and keeps functionality outside the markup code) instead of adding this to the end of the body of the HTML.

    The article you point to is somebody's opinion that is not agreed with everybody.

    Unobtrusive JavaScript !== adding JavaScript in the bottom of the body

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Spot on, an external file is how it should be done in the first place. As you may be fully well aware, some things are left out for the sake of clarity.

    Code javascript:
    <html>
    <head>
    </head>
    <body>
    ...
    <script src="myscript.js"></script>
    </body>
    </html>

    myscript.js
    Code javascript:
    var target = document.getElementById("berenger");
    alert(target.nodeName);

    Peter's Blog does an excellent job of taking you through the many and varied techniques relating to the window.onload problem
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •