Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-25-2011, 09:19 PM   PM User | #1
wuzhannanan
New Coder

 
Join Date: Jul 2009
Location: Chicago, IL
Posts: 54
Thanks: 5
Thanked 0 Times in 0 Posts
wuzhannanan is an unknown quantity at this point
window.onload from two functions breaks my code

Hi, I have my page here. There is a tab system on the page that uses javascript. View the source the see the javascript file (tabber9.compressed.js).

Now, on my end, I am adding some tracking code. The code looks like this:

Code:
<SCRIPT TYPE='text/javascript' LANGUAGE='JavaScript'><!--    //
    window.onload = initPage;
    function initPage() {
        if (this.GetCustomerGUID)
            document.forms["Test"].elements["CustomerGUID"].value = GetCustomerGUID();
    }
//--></SCRIPT>
The problem is, when I add this code to my footer, it breaks the tab system. The tabs don't work any more.

I am not a JavaScript expert by any means (or else I would have known what the issue is! lol) but, I guess it has something to do with the window.onload function? I say that because I see the window.onload code in the tabber javascript as well.

Any advice or help anyone can give would be greatly appreciated. Thanks in advance for any help!
wuzhannanan is offline   Reply With Quote
Old 05-25-2011, 10:02 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Instead of using window.onload, you can *add* your onload event handler to the existing handlers.

But another way is to simply call one of the onload initializations from the other.

For example, if you have:
Code:
window.onload = setupTabbber;
...
window.onload = initPage;
...
The second onload will, as you noted, wipe out the first one.

So just call the first one from your initPage, thus:
Code:
   function initPage() {
        setupTabber( );
        if (this.GetCustomerGUID)
            document.forms["Test"].elements["CustomerGUID"].value = GetCustomerGUID();
    }
The other way to do it is to create an onload-appender, but now you get into the world of browser dependencies.

Code:
function addOnLoad( functionToCall )
{
    if (window.addEventListener) {
        window.addEventListener('load', functionToCall, false);
    } else if (window.attachEvent) {
        window.attachEvent('onload', functionToCall);
    }
} 

... and then do ...

addOnLoad( setupTabber );
...
addOnLoad( initPage );
...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 05-26-2011, 03:59 PM   PM User | #3
wuzhannanan
New Coder

 
Join Date: Jul 2009
Location: Chicago, IL
Posts: 54
Thanks: 5
Thanked 0 Times in 0 Posts
wuzhannanan is an unknown quantity at this point
OK, I tried adding setupTabber( ); to the initPage function like you described, and it did not solve my problem. Do I also need to modify the tabber js?
wuzhannanan is offline   Reply With Quote
Old 05-26-2011, 08:52 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by wuzhannanan View Post
The problem is, when I add this code to my footer, it breaks the tab system. The tabs don't work any more.
take out the onload wrap: if your script is at the bottom, you don't need it anyways because all the needed elements will be on the page by the time the browser gets to processing the footer.

likewise, you could probably init the tabs immediately at the bottom of the page too, right above the tracker code. i doubt either one needs to wait all the way until onload to run it's code. Typically, it's only when working with images or dynamically sizing containers that have images when waiting for window.onload() is worth it.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, window.onload

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:27 AM.


Advertisement
Log in to turn off these ads.