At 02:39 PM 1/13/05, Ben Curtis wrote:
Also, while it's convenient to insert javascript event handlers into
HTML
markup when demonstrating an example, in practice it's probably best
to
leave the script out of the markup and apply it from a separate
script file
at window.onload.
One beef I have with this code, and most code of this nature, is that
it uses this to trigger it:

        window.onload = externalLinks;

This is fine, if it's the only code you are assigning to onload, but it
overwrites any previous onloads and is overwritten by subsequent
onloads.


Ben,

Wow, your method looks elaborate. When I have some more time I'll go through it in detail and try to give you some feedback.

In the meantime, the way I get around the one-function-onload problem is simply to load a generically-named function at the global level:

        window.onload = jsInit;

Then, jsInit() can be defined differently by each page to load its own batch of functions:

        function jsInit()
        {
                var x = doThis();
                var x = doThat();
        }

This method isn't *ideal* because I have to list all of the onload functions manually for each page, instead of simply attaching the functions to the html file with <script> tags. But it works.


Another method I've imagined but never implemented is for each added function to add itself to an array of functions. That is, instead of each attached function using:
window.onload = runMe;
instead it uses:
windowOnload(windowOnload.length) = runMe;
to add itself to a global array:
var windowOnload = new Array();


Or:
windowOnloadAdd(runMe);
where windowOnloadAdd() is a global function that appends each function to the array.


Then, finally, the global statement:
window.onload = runOnloadFunctions;
where runOnloadFunctions() executes each function in the windowOnload() array (if any).


Like your method, this involves replacing the window.onload statement for each desired function and leaves the rest to global code.

Paul

******************************************************
The discussion list for  http://webstandardsgroup.org/

See http://webstandardsgroup.org/mail/guidelines.cfm
for some hints on posting to the list & getting help
******************************************************



Reply via email to