Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.Onload = function( ) ??? 1

Status
Not open for further replies.

budapestnori

Technical User
Jan 15, 2004
33
HU
Ok, I am sure this will be easy for some, but is has me stumped. I had had a web page with a call to a javascript function in the body tag working great. The function passed some params to an included js file which was inserted up in the body. Next, I came across a high-tech site that had a cool sroll effect to a clipped paragraph div on the page. Are you with me? I cut out this feature and was able to get it to work in and by itself on a page. Good for me I thought, but when I copy all the code into one of my existing pages, the clipped pararaph appears but doesn't scroll. I located the problem, but don't know how to fix it. Apparently, in the head tag, there is this code:

window.onload = function(){
myScroll = new Scroller(10,18,76,'track','up','down','drag','contentMask','content');
};

Further down in my body tag I have this code:

onload="mbSet('mb2', 'mbh');"

When I take the body onLoad call out code out, the scroll one works, leave it in, and the scroll one won't scroll. Is this an easy fix? I hope so.
 
Change one of the onloads into a proper function, and then add it to the body onload. Let me show ya what I mean :)

Take this code:

Code:
window.onload = function(){
myScroll = new Scroller(10,18,76,'track','up','down','drag','contentMask','content');
};

And change it to this:

Code:
function initScroll()
{
  myScroll = new Scroller(10,18,76,'track','up','down','drag','contentMask','content');
}

Then append a call to this from the body tag:

Code:
<body onload=&quot;initScroll();mbSet('mb2', 'mbh');&quot;>

I hope this does the trick for you!

Jeff
 
You were having problems because as the page was loading the <BODY onload=&quot;...&quot;> overwrote what you already had in there. Another option is to define a blank function, then use the Function object's apply() method to add stuff to the new function:

window.onload=function(){};
window.onload.apply(doload1())
window.onload.apply(doload2())

This is great if you have multiple external .js script files, each having an onload function. You can add code like this to each .js file.

if(window.onload==null){window.onload=function(){}}
window.onload.apply(myFunc1())
window.onload.apply(myFunc2())

If you use the onload event like this, make sure you don't use it in combination with <BODY onload=&quot;...&quot;> as the BODY one will overwrite what's already there.

P.S. I created an FAQ about this for future reference: faq216-4862

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
Umm, you can ignore my post. It turns out that my script wasn't waiting for the page to load before executing the functions. I was way off. I'm still looking for a good way to use multiple window.onload's. I'll let you know if I have any luck.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
Adam,

I had created such a function a while back:
Code:
/************************************************
*	DYNAMIC WINDOW.ONLOAD
*	allows a dynamic number of scripts to be called onload
*
*	useful if your header does something onload,
*		and you need another file
*		to do something onload also
*
*	e.g.
*	window.addOnload( foo );
*	window.addOnload( function() { foo(arg); } );
*	window.addOnload( function() { bar(a, b, c); } );
*/

window.addOnload = function (fn) {
	if (!window.OnloadCache) window.OnloadCache = [];
	var ol = window.OnloadCache;
	ol[ol.length] = fn;
}

window.onload = function () {
	var ol = window.OnloadCache;
	if (ol)
		for (var x = 0; x < ol.length; x++)
			this.action = ol[x]();
}
/*
*
**************************************************/


hope it helps

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Thanks, Jeff. Do you mind if I replace my code with yours in the FAQ? I'll make sure you get credit.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top