SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru quenting's Avatar
    Join Date
    Dec 2002
    Location
    Switzerland
    Posts
    735
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can onload event be called from anywhere else than body tag ?

    see subject

    Quentin

  2. #2
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    onload is only valid for use with body and frameset elements.

    Some browsers (possibly most) will accept it with the <img> tag, but while it will work, it will invalidate the code.
    There are possibly other elements/tags with which it will 'work', but body and frameset remain the only valid uses.

    This goes for HTML and XHTML.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Originally posted by Bill Posters
    onload is only valid for use with body and frameset elements.

    Some browsers (possibly most) will accept it with the <img> tag, but while it will work, it will invalidate the code.
    There are possibly other elements/tags with which it will 'work', but body and frameset remain the only valid uses.

    This goes for HTML and XHTML.
    It also works with the window object:
    Code:
    window.onload = function () {
    //disable all form elements.
    var el = document.form1.elements;
    for (var x = 0, var l = el.length; x < l; x++) {
        el[x].disabled = true;
      }
    }
    I am not too sure how "valid" this code is though, like Bill Posters said.

  4. #4
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also do it this way in the head section:
    function myfunc()
    {
    some script
    }

    window.onload=myfunc

    Notice that you leave off the "()" when calling the function. If you need to call multiple functions from the onLoad in the body tag you separate them with a semi colon like this:
    <body onLoad="myfunc1();myfunc2();myfunc3()">

    Hope that helps.


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
  •