supposing you had multiple scripts running, many remote hosted and you cant remember how many or which ones used a body onload tag.
Could you use a wildcard to cover them? for example, is something like this possible?

<body onload="popWindow();somefunction();"*"(); "*"();">

:?:

Recommended Answers

All 6 Replies

O_O

the wildcard * generally relates to everything. If that was possible, your code would be saying "call every function you can find, and pass no parameters".

it would be more handy if you could go halfway, like onload = "wildfnc('_onload*')" to execute all functions prefixed with _onload....

But, unfortunately no. No such thing exists unless you implement it yourself.

The best way to manage such a thing is to append events to your body object from code rather than write them all in the onload attribute; that or call all of your onload functions from a single onload function.

Using a single onload function is the best way to go. This javascript function allows you to keep adding functions to the onload event without having to worry about the ones already added:

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

Use like this to add a function to the onload event:

addLoadEvent(functionName1);
addLoadEvent(functionName2);

Thankyou that looks easier! Do I need to add numbers and increment or just keep adding with a ';' at the end of each?

<script>
function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } }
addLoadEvent(popwindow);
addLoadEvent(spellchecker);

</script>

Using a single onload function is the best way to go. This javascript function allows you to keep adding functions to the onload event without having to worry about the ones already added:

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

Use like this to add a function to the onload event:

addLoadEvent(functionName1);
addLoadEvent(functionName2);

You have it perfect. Good luck. :)

Thanks Again! :D

Using a single onload function is the best way to go. This javascript function allows you to keep adding functions to the onload event without having to worry about the ones already added:

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

Use like this to add a function to the onload event:

addLoadEvent(functionName1);
addLoadEvent(functionName2);

I hope you don't mind another post on this subject so long after the initial post. I found myself in need of a way to handle multiple onload functions and found your post.
I am NOT an experienced javascript coder, I just know enough to get by. This is further complicated by the fact that I'm a programmer, and need to be able to implement this within a perl script.
So, I'm a little confused about the addLoadEvnt(functionName1) statements to add additional functions to the onLoad. If all I'm putting in there is the name of a function, then where are all the actual functions stored for it to pull the actual code in? I have several "routines" - one is a Timer function that calls a secondary function; another is a function to try to disable certain Ctrl+key combinations that are causing major programming problems (it also calls one or more functions); and there is at least one more styleswitcher function that has its own window.onLoad calls.
If I put the primary function into the addLoadEvnt - then do I store that function, as well as the other functions that IT calls in a common javascript file pulled in via the <script type="text/javascript" ... statement at the top of the page?

I hope I'm making sense!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.