SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Problems running code onload

    Hi guys,

    This is probably the most ridiculously dumb question ever posted on this Javascript forum, but I'm really at a bit of a loss. I currently have the following code to hide a div:

    Code JavaScript:
    function hide(element){
    	element.style.position = 'absolute';
    	element.style.left = '-4000px';
    }
    Very simple, yet effective and lightweight so I'm fine with it.

    If I call it using onclick with the following code, it works fine:
    Code JavaScript:
    onClick="hide(specific)"

    Yet, at the top of my page I have this, which isn't activating at all for some reason:
    Code JavaScript:
    window.onload = hide(general);

    The problem isn't with the window.onload bit, because I've tested it with an alert, nonetheless it isn't activating the hide function and I can't understand why

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That WILL give you an error, because that's not how you're supposed to add an onload event listener. Instead, do something like the following:

    Code:
    function loadPage() {
      hide(general);
      return;
    }
    
    window.onload = loadPage;
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively:

    Code:
    window.onload = function() {
      hide(general);
      return;
    }
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You're a star, thank you so much pip!

  5. #5
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NP. Glad to help
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.


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
  •