PDA

View Full Version : can't run function on page load



gib65
07-19-2010, 08:28 PM
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):



<body style="background-image: url('background.jpg');" onpageload="javascript:getFilenameFromURL();">


and here's my function:



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/dynamicindex4/cmotiongallery.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

jscheuer1
07-20-2010, 07:39 AM
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:


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:


onpageload="javascript:getFilenameFromURL();"

That actually does nothing.

and get rid of the:


window.onload=fillup;

from the other script.

Now you can put them together in the body tag:


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

gib65
07-20-2010, 04:50 PM
Once again, John, you made my day. Thanks for the great tip.