SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with the onload event

    I'm having a wierd problem with the body-onload event. When I use the onload event directly in the body tag, I can access all the elements in the body and alter them. The problem is, I want to keep all the javascript in external JS-files, and for some uknown reason "document.body.onload = someFunc();" doesn't work. It seems like the function is executed before the content of the body is loaded.

    Anyone have any ideas how to make this work?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe the body doesn't exist when you try to assign the handler.

    I always use window.onload, give that a shot.

  3. #3
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tried it, didn't work. But I did find a solution to my problem:
    Code:
    window.onload = function() {
       editSomeTag();
    }
    Though, I find it strange that "window.onload = someFunc();" doesn't work. Thanks for your help anyway

  4. #4
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lix0r View Post
    tried it, didn't work. But I did find a solution to my problem:
    Code:
    window.onload = function() {
       editSomeTag();
    }
    Though, I find it strange that "window.onload = someFunc();" doesn't work. Thanks for your help anyway
    It doesn't work because someFunc() executes the function and assigns its result to be window.onload's value. If someFunc() returns another function it is perfectly fine, however it sounds like in your case it does not. Lose the parenthesis.

  5. #5
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, that makes sense. Thank you for the heads up


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
  •