Results 1 to 3 of 3

Thread: can't run function on page load

  1. #1
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default can't run function on page load

    Hello,

    I'm trying to run a javascript function on the website load. The website features a few things, however, that seem to be making this difficult.

    Here's index.html (the relevant part):

    Code:
    <body style="background-image: url('background.jpg');" onpageload="javascript:getFilenameFromURL();">
    and here's my function:

    Code:
    function getFilenameFromURL() {
    
    window.alert("hello world");
    
    }
    As you can see, it's only built for testing purposes at the moment.

    As it stands, this code doesn't do anything. I'm not getting the "hello world" alert on the page load that I would expect. On the other hand, I've been told to use onload instead of onpageload in the body tag, but that's where the interference with other page elements seems to come in. I'm using one of dynamic drive's motion galleries (this one: http://www.dynamicdrive.com/dynamici...iongallery.htm) and if I use onload, the gallery becomes unscrollable for some reason (but I do get my alert).

    So I can have either a functional motion gallery or a functional onload function, but not both.

    I would like to have both. Can anyone suggest why this is proving difficult?

    Here's my website: http://www.surrealsource.com

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,979
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    There are several variations/types of ways to assign a function to the onload event. Some of these are mutually exclusive. If you only use that type, whichever one is seen last by the browser is the only one that will be executed.

    If you look inside the motiongallery.js script, you will see this line near the end:

    Code:
    window.onload=fillup;
    That's one of the exclusive ways I was talking about. Using onload in the body tag is another. But you can combine them into one onload in the body tag -

    Get rid of this:

    Code:
    onpageload="javascript:getFilenameFromURL();"
    That actually does nothing.

    and get rid of the:

    Code:
    window.onload=fillup;
    from the other script.

    Now you can put them together in the body tag:

    Code:
    <body style="background-image: url('background.jpg');" onload="fillup();getFilenameFromURL();">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Once again, John, you made my day. Thanks for the great tip.

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
  •