Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: Onload

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Onload

    I have this code:
    Code:
    <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c7b22470e521876"></script>
    I want this JavaScript code above to be executed when all My page finished loading (maybe by using JavaScript onload)
    So how can I do that?
    Last edited by Tony M; 08-30-2010 at 08:17 AM.

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    You cant execute the js-file itself, but you can execute a function within it this way:

    <body onload="FunctionName();">

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Put your code at the end of the page , just before </html>

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    Quote Originally Posted by DaveyErwin View Post
    Put your code at the end of the page , just before </html>
    NO!

    for two reasons:

    Firstly the document is not fully loaded in until the </html> is reached, and therefore the code is executed during the load fase.

    Secondly HTML do not support anything between the </body> and the </html> tags.
    -------------------------
    The structure of a HTML is roughly:

    <!DOCTYPE>
    <html>
    <head>
    title, metas, scripts and stylesheet goes here
    </head>
    <body>
    text ,table, divs etc. goes here.
    </body>
    </html>
    some scripts (excl functions) can be located in the BODY, if the are not affected by any other body content during the load-fase.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    yes, you are correct.
    the script block should be
    just before the body closing
    tag just as it is at www.msn.com

  • #6
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    That script is what I will call a tracker-script.
    I has nothing do with the rest of the document.
    It provide microsoft with some information of your browsing behavior, that they can use in statistics. Such as if you typed the address in or you followed a link.

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    here is what Peter Michaux has to say....

    The idea is that by the time the browser parses this final script element and runs the contained JavaScript, the elements that precede this final script element will be parsed and available as part of the DOM. This assumption based on de facto standard browser behavior and may not satisfy some developers following the letter of specifications. Many developers do depend on this behavior working in many scripts.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    
    <head>
      <title>Search Engines</title>
      <script src="behavior.js" type="text/javascript"></script>
    </head>
    
    <body>
      <h1>Search Engines</h1>
      <ul>
        <li id="google">Google</li>
        <li id="yahoo">Yahoo!</li>
      </ul>
      <p><img src="hawaii.jpg" alt="hawaii"></p>
      <script type="text/javascript">init();</script>
    </body>
    
    </html>behavior.js
    
    function init() {
      document.getElementById('google').onclick = function() {alert('Google');};
      document.getElementById('yahoo').onclick = function() {alert('Yahoo!'););
    }
    It is even difficult to see the seven characters of JavaScript at the bottom of the HTML page. This is relatively good separation...


    you will notice nothing is said of the dom not being ready

    also can you provide a brief example demonstating any problem

    de facto standard browser behavior means this is the way
    browsers really work

  • #8
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    if you look at the content of the function it specifies what must happen if a user takes an action and clicks on one of the related element.


    the function could just as well have been triggered by <body onload="init();">
    which is the proper way to do it.

    In both cases, nothing will happen if the user clicks on the elements before the page has loaded,

    But That's how microsoft are:
    Always setting their own standards of what is proper coding.

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    yes you are right, so
    it really should be
    written like this ...

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    
    <head>
      <title>Search Engines</title>  
    </head>
    
    <body>
      <h1>Search Engines</h1>
      <ul>
        <li id="google">Google</li>
        <li id="yahoo">Yahoo!</li>
      </ul>
    <script type="text/javascript">
      document.getElementById('google').onclick = function() {alert('Google');}
      document.getElementById('yahoo').onclick = function() {alert('Yahoo!');}
    </script>
      <p><img src="hawaii.jpg" alt="hawaii"></p>  
    </body>
    </html>
    so no delay

  • #10
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    Functions triggered by user-clicks can only be executed after the page has fully loaded, so this change will have no effect!

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    
    <head>
      <title>Search Engines</title>  
    </head>
    
    <body>
      <h1>Search Engines</h1>
      <ul>
        <li id="google">Google</li>
        <li id="yahoo">Yahoo!</li>
      </ul>
    <script type="text/javascript">
      document.getElementById('google').onclick = function() {alert('Google');}
    document.getElementById('google').innerHTML = "hiyas";
      document.getElementById('yahoo').onclick = function() {alert('Yahoo!');}
    </script>
      <p><img src="hawaii.jpg" alt="hawaii"></p>  
    </body>
    </html>
    you will never see google
    (unless your javascript is off)
    all you will see is hiyas
    no user reaction

  • #12
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    your added line will work since it requres no user action.

    But why not use:
    <li id="google">hiyas</li>
    instead of changing it shortly after????


  •  

    Posting Permissions

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