How can I add body onLoad inside JavaScript?

If you're jose_juan, you'll recognize the attached script as your own.

I want to add this to a bunch of pages with a simple <script> tag pointing to a .js file.  How can I add the onLoad event of body to the .js file?  SImply doing:

body.onload = SetBlank2Ya(document.body);

results in an error.


function SetBlank2Ya(o) {
        for( var i = 0; i < o.childNodes.length; i++ ) {
                var c = o.childNodes[i];
                if( typeof( c.childNodes ) != 'undefined' )
                        SetBlank2Ya(c);
                if( c.tagName == 'A' && c.target == '_blank' ) {
                        // c.href = '';
                        c.target = '_blank';
                        c.onclick = function() { alert('Ya'); };
                }
        }
}
 
 
// And in the body tag:
// body onLoad="SetBlank2Ya(document.body);"

Open in new window

mrcoulsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
try:
window.onload=function(){ SetBlank2Ya(document.body); };
0
mrcoulsonAuthor Commented:
Awesome!  Now, how do make it add that to the exisitng onLoad event if there is one?  I'm noticing it will either override or be overridden by existing onLoad events.
0
hieloCommented:

//this does nothing useful. Just a "placeholder"
var temp=function(){ return 1;};
 
if( window.onload )
{
 //override "placeholder" with whatever already exists in onload
  temp = window.onload;
}
window.onload=function(){ SetBlank2Ya(document.body); temp();};

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mrcoulsonAuthor Commented:
I have tried

window.onload+=function(){ offSiteAlerter(document.body); };

That just seems to stop the script from working altogether.
0
mrcoulsonAuthor Commented:
Oh, you posted that before I got to read it.  Please hold...
0
mrcoulsonAuthor Commented:
Awesome!!

Now, do you have two minutes to explain it?
0
hieloCommented:
temp will always have a function reference. The function reference will either be:
a. The "useless" place holder I defined OR
b. the original/pre-existing window.onload

so you just override the "place-holder" reference if there is a pre-existing window.onload. So after the if, temp will have a function reference no matter what. So you can just make a function call within the new anonymous function being assigned to window.onload.
0
mrcoulsonAuthor Commented:
I think I get it!  Thanks a ton!
0
hieloCommented:
you are welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.