[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

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

0
mrcoulson
Asked:
mrcoulson
  • 5
  • 4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now