Forum Moderators: open

Message Too Old, No Replies

safe to use body onload for all pages, even those that don't need it?

for the sake of template-based equilibrium

         

dannyboy

1:52 am on Apr 24, 2004 (gmt 0)

10+ Year Member



About 2/5 of my pages need the onload event handler for various script functions.

Assuming all the body tags are the same:

<body onload="loadfuncs();">

while I load all the scripts within a separate .js file:

function loadfuncs() {
functionA('a');
functionB('b');
functionB('c');
functionB('d');
}

Question: is it "safe" to implement this global onload event handler for all pages, even those that don't implement any onload script functions (ie, popups, printer-friendly page, etc)?

Another reason I want the body onload function available on every page is because I may need to implement some global onload function in the future. I'm basically trying to plan ahead and future-proof my site.

Thanks for any advice.

Rambo Tribble

2:42 am on Apr 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As long as the handler doesn't call anything, it should be safe. It isn't really going to save you much work though, since you will have to go to every page and enter the function name when the time comes.

Alternatively, you could use a function name, then just create a function that does nothing, such as:

function doNothing(){
//nothing happening here
}

You do want to avoid calling a function that doesn't exist, as that could produce an error message.

dannyboy

4:03 am on Apr 24, 2004 (gmt 0)

10+ Year Member



Well, since I'm using a single universal onload event in the <body> tag, while stacking the onload events in an independent .js file I would never have to update my <body> tag, regardless of how many new onload functions I add.

I understand what you're saying about using a silent onload function that doesn't exist, but that's not exactly what I'm thinking of.

All my pages will link to the same .js file. This .js file will have about a half dozen function specified which will be executed by stacking them in the .js file:

partial .js file content:

function loadfuncs() {
functionA('a');
functionB('b');
functionC('c');
functionD('d');
functionE('e');
functionF('f');
}

my concern is if I open a page that doesn't use any of those functions, could this lead to problems?

For instance, half my site will use all 6 functions, while the other half will use none, but all the pages contain the following:

<body onload="loadfuncs();">

Is this asking for trouble?

Rambo Tribble

5:16 am on Apr 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the .js file is linked and the function is called by onload, the function will run. What the function does after that depends on how you program it. You can structure it to abort gracefully, or not, but if you call the function, it's gonna run. It will not run if 1) you don't call it, or 2) it doesn't exist (as in file not linked).