Can A Website Have Multiple Onload Functions

Suppose my web site consists of four web pages. When each of the four pages loads, different functions are called to do different things and the functions being called are specific to each page.

If I put all the functions that all four pages will call into an Onload function I’m afraid they will all needlessly execute and waste time.

So my question is do I have to have multiple Onload functions in different javascript files to ensure that only the right functions are executed when each of the pages loads and if so would they interfere with one another.

You could store your onload functions in variables and use some conditional check of the window.location url string to determine which function to execute.

Hi, Thanks for your reply. What if of the four web pages three are web pages that will be displayed in the Object element on my index.html page, will window.location be able to get their url? Also, since they are displayed as embedded web pages inside of the index.html page, will the Onload function execute when they load?

I think that a document loaded in an iframe (embedded) only has access up to the document root of that iframe. That iframe should have it’s own headers though.

This codepen demonstrates the pen containing an iframe of another pen. Both snippets alert window.location. The iframe snippet alerts it’s own address independent from the containing snippet.

If you include the onload script on the individual pages, they will execute when the embedded page loads, also demonstrated by the snippet. If you still want to keep the onload scripts contained in a single file, then you can just check the src attribute of your iframe (I am assuming this is how you are embedding the pages).

Thanks for your reply. Your codepen example shows an iframe but I’m using an Object element. I’ve looked around online and I just don’t see if window.location will return the url inside of the Oject element.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.