www.webdeveloper.com
Results 1 to 7 of 7

Thread: hellllllppppp!!! i must have no clue...

  1. #1
    Join Date
    Aug 2010
    Posts
    24

    Angry hellllllppppp!!! i must have no clue...

    can someone explaine to me why this doesnt work??? i've read through 3 books on javascript before ever even attempting it. however, i'm realizing that none of what i do really works. so i'm guessing there was a chapter i may have missed and someone can help me. this isnt a script that im using, but it is an example that i made up to show you guys quickly what i am talking about.

    <html>
    <head>
    <script type="text/javascript">
    var doSomething=document.getElementById("do");
    doSomething.onclick=getinfo;
    function getinfo() {
    alert("talk about me");
    }

    </script>
    </head>

    <body>

    <a href="#" id="do">whateveer</a>


    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    You're telling it to do something BEFORE you have told it where to do it from.
    Try this two line change...
    Code:
    <html>
    <head>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=234134
    
    onload = function() {
      var doSomething=document.getElementById("do");
      doSomething.onclick=getinfo;
    }
    
      function getinfo() { alert("talk about me"); }
    
    </script>
    </head>
    <body>
    
    <a href="#" id="do">whateveer</a>
    
    </body>
    </html>

  3. #3
    Join Date
    Sep 2008
    Posts
    408
    Reorder:

    HTML Code:
    <html>
    
    <body>
    
    <a href="#" id="do">whateveer</a>
    <script type="text/javascript">
    var doSomething=document.getElementById("do");
    doSomething.onclick=getinfo;
    function getinfo() {
    alert("talk about me");
    }
    
    </script>
    </body>
    </html>
    Or add an onload:
    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    window.onload = function() {
      var doSomething=document.getElementById("do");
      doSomething.onclick=getinfo;
      function getinfo() {
        alert("talk about me");
      }
    };
    </script>
    </head>
    <body>
    
    <a href="#" id="do">whateveer</a>
    
    </body>
    </html>
    While you're trying to assign doSomething = 'do'; 'do' doesn't exist yet.

    Edit: Yeah, what JMRKER said

  4. #4
    Join Date
    Aug 2010
    Posts
    24
    thanks.... so its the onload handler that i needed. but, if im not mistaken event handlers usually activates a function. like you have above. so does that mean that my entire code will be a sequence of functions? how would i ever be able to call a variable globally? or even better, how do i escape the function?

    ex.
    Code:
    window.onload=goodness;
    function goodness() { 
        document.write("hello world")
    }
    
    var a=6
    var x=9
    in other words, if the browser needs me to tell it to wait til the page has loaded before executing my script, will what comes after the function that was executed through the onload handler happen also after the page has loaded? or will it make the two variables first then call the onload?

  5. #5
    Join Date
    Sep 2008
    Posts
    408
    The onload will execute that function after the page is completely loaded (images included) so it will run only once. You don't need to use an onload for this example, you just need to make sure that the 'do' exists in the DOM before your try to read it using getElementById('do').

    To work with a global variable, initiate it globally and then inside the function use it:

    HTML Code:
    <script type="text/javascript">
    var doSomething;
    window.onload = function() {
      doSomething=document.getElementById("do");
      doSomething.onclick=getinfo;
      function getinfo() {
        alert("talk about me");
      }
    };
    </script>
    What you could do is also set up a setTimeout to see if document.getElementById("do") exists yet. However, the best would probably just put at the bottom of your <BODY> without the onload as it will be available instantly.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    Try this as a test...
    Code:
    <html>
    <head>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=234134
    
    // Try substituting next line for below as a second text
    //  var a=6;  var x=9;
    
    onload = function() {
      var doSomething=document.getElementById("do");
      doSomething.onclick=getinfo;
    }
    
      function getinfo() { alert("a = "+a+"\nx = "+x); }
    
      var a=6;   var x=9;
    // Remove above to see effects of moving global variables
      
    </script>
    </head>
    <body>
    
    <a href="#" id="do">whateveer</a>
    
    </body>
    </html>
    You should see no difference with the substitutions indicated.

  7. #7
    Join Date
    Jun 2007
    Posts
    667
    Code:
    <html>
    <body>
    
    <a href="#" id="do">whatever</a>
    
    <script type="text/javascript">
    var doSomething=document.getElementById("do");
    doSomething.onclick=getinfo;
    function getinfo() {
    alert("talk about me");
    }
    
    </script>
    </body>

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