www.webdeveloper.com
Results 1 to 13 of 13

Thread: onLoad in head and body

  1. #1
    Join Date
    Aug 2004
    Posts
    2

    onLoad in head and body

    I have an onLoad command in the head and another in the body, but only the second one works. I am assuming the second one cancels out the first. Is there a way around this?

  2. #2
    Join Date
    May 2004
    Location
    Manhattan NY
    Posts
    6,028
    Put both in the body tag.

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Or just couple the two in the documents HEAD.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Aug 2004
    Location
    Zimbabwe
    Posts
    68
    Or use something more sophisticated, like Simon Willison's addLoadEvent().

    http://www.sitepoint.com/blog-post-view.php?id=171578

    btw they're not 'commands', but simple variable assignments (to an event handler property) - and the second assignment is simply over-writing the first.

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <meta name="Content-Style-Type" content="text/css">
    <title>Example</title>

    <script type="text/javascript">
    <!--
    onload = function() {
    first();
    second();
    }
    // -->
    </script>

    </head>
    <body>

    </body>
    </html>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    May 2004
    Location
    Manhattan NY
    Posts
    6,028
    What's the big deal about putting 2 calls in the body tag? I do it all the time. Or I put them both in the head with the calls: window.onload=function() {'functionName()'};

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Nothing, really. But note, my example above is a reoly to a post that has since been deleted.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Nov 2002
    Location
    Nashua, NH
    Posts
    3,195
    onload = function()
    { /*Initialize*/
    }

    is fine if you are scripting for yourself. However, if you plan on sharing the script, this can cause trouble when combined with other script that use body onload.

    Having initialization function that is added to <body onload=""> not only avoids such problems, but also gives you freedom to initialize script using other events, which can be used for demo/debugging purposes.

  9. #9
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Originally posted by Vladdy
    onload = function()
    { /*Initialize*/
    }

    is fine if you are scripting for yourself. However, if you plan on sharing the script, this can cause trouble when combined with other script that use body onload.

    Having initialization function that is added to <body onload=""> not only avoids such problems, but also gives you freedom to initialize script using other events, which can be used for demo/debugging purposes.
    Your point is well taken. It's just that I'm not a big fan of people using scripts without understanding them and I'm rather fond of using:

    if (document.getElementById) onload = function () {}
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  10. #10
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Perhaps I start using:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <title>Example</title>

    <script type="text/javascript">
    <!--
    myScript = !document.getElementById ? function () {} : function () {
    ...;
    ...;
    ...;
    }
    // -->
    </script>

    </head>
    <body onload="myScript()">

    </body>
    </html>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  11. #11
    Join Date
    Nov 2002
    Location
    Nashua, NH
    Posts
    3,195
    I see your reasoning, Charles.
    My point is: if a person installing a script, does not understand the basic:
    Code:
    /* ---- SCRIPT INITIALIZATION ----
     * Foo script is initializied using foo.init() method of the foo object
     * Foo script uses existing page elements and enhances their functionality,
     * therefore it should be initialized after the page has loaded.
     * The most common method that satisfies this requirement is adding a 
     * call to the foo.init() method to the onload attribute value string
     * of the document body element:
     *      <body onload="foo.init()">
     *----------------------------------*/
    he/she should not be doing it in the first place.

    P.S.
    I would rather provide information about initialization function and allow competent web masters use it as they see fit (maybe they need to run a few conditional statements before deciding to call it), than hard code initialization and sacrifice flexibility only to "benefit" a few dumb ones.
    Last edited by Vladdy; 08-21-2004 at 10:39 AM.

  12. #12
    Join Date
    May 2004
    Location
    Manhattan NY
    Posts
    6,028
    I have had to put calls inside the script tags before to get pages to validate. I was using onresize(), which apparently stops certain doctypes of HTML 4.01 from validating.

  13. #13
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    I think that you will find that "onresize" is invalid for all types of HTML and XHTML&trade;.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

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