SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: body's parent

  1. #1
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    body's parent

    Hi

    Code:
    <head>
    <script type="text/javascript">
    body.onload = foo_load;
    body.onunload = foo_unload;
    </script>
    </head>
    <body>
    </body>
    This isnt correct since I got to specify body's parent.
    I tried window and document but its still an error according to mozilla's error console.

    How do I correct this ?

    Thanx
    Anjanesh

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    window.onload = foo_load;
    window.onunload = foo_unload;

    The events belong to the window. THe only reason they can be added inline in the body tag is that there isn't a tag for the window and the body is the closest equivalent.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will continue to get errors if you have not defined foo_load and foo_unload
    To attach an event to the body use document.body, there is no window.body.
    felgall is right - use the window for load and unload handlers.
    Last edited by mrhoo; Jan 21, 2007 at 20:23.

  4. #4
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh...Thanks...I didnt know that even body wasn't to be mentioned.
    Normally I would've given it as
    Code:
    document.getElementById("element-id").onload = foo;
    Anjanesh

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You can attach event handlers to elements in the page using that method but only after the page finishes loading or at least after the tag with that id has loaded. This means that to use that code you either need to place it inside of the foo_load function from your earlier example or set up a loop to test for when the id exists so that you can add the event handler to the tag with that id just as soon as the tag is loaded. See http://javascript.about.com/library/blfastdom.htm for one way to link it in as soon as the tag is loaded.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    window.onload = load() is obviously not right since parentheses is not allowed - othewise it gets executed and assigned to window.onload.
    But I need to specify args to my load function window.onload = load(x, y)
    How do I get this done ?

    PS:
    These 3 links demonstrate excellent examples of how to do an on-DOM-load instead of on-window-load :
    http://dean.edwards.name/weblog/2005/09/busted/
    http://dean.edwards.name/weblog/2006/06/again/
    http://www.outofhanwell.com/blog/ind...blem_revisited
    Anjanesh

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    window.onload = load(x, y)
    window.onload = function(){load(x, y)}

    Remember that load, x and y must be defined before onload is called
    Last edited by mrhoo; Jan 22, 2007 at 09:40.

  8. #8
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This was what I tried initially but I got a second function thats needs to load on window.onload.
    Code:
    window.onload = init; // Defined in the beginning
    Code:
    window.onload = load; // load(x, y), defined somewhere much later in the page
    The latter has to be defined later. [Google Maps function - setting lat, long as center of map]
    Anjanesh


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
  •