PDA

View Full Version : Getting around Javascript Onload Functions


72dpi
07-11-2006, 08:52 AM
Ok,

i know you are gonna LOVE this hack.

trying to find a workaround for my current font resizer hack, I was thinking about how to implement javascript which uses an "onload" function, and applying it without having to edit any templates.

Thanks to Brothercake http://www.brothercake.com/site/resources/scripts/onload/
There is a function to Load javascripts which normally use <body onload="blahfunction();">

Wanna know how to do it?

here you go:

save this as onload.js

//GO1.1


///////////////////////////////////////
//
//**Generic onload by Brothercake
//**http&#58;//www.brothercake.com/
//
///////////////////////////////////////



//onload function
function generic&#40;&#41;
{
********// add your function here
****alert&#40;&#39;Generic onload function&#39;&#41;;
};



//setup onload function
if&#40;typeof window.addEventListener &#33;= &#39;undefined&#39;&#41;
{
****//.. gecko, safari, konqueror and standard
****window.addEventListener&#40;&#39;load&#39;, generic, false&#41;;
}
else if&#40;typeof document.addEventListener &#33;= &#39;undefined&#39;&#41;
{
****//.. opera 7
****document.addEventListener&#40;&#39;load&#39;, generic, false&#41;;
}
else if&#40;typeof window.attachEvent &#33;= &#39;undefined&#39;&#41;
{
****//.. win/ie
****window.attachEvent&#40;&#39;onload&#39;, generic&#41;;
}

//** remove this condition to degrade older browsers
else
{
****//.. mac/ie5 and anything else that gets this far
****
****//if there&#39;s an existing onload function
****if&#40;typeof window.onload == &#39;function&#39;&#41;
****{
********//store it
********var existing = onload;
********
********//add new onload handler
********window.onload = function&#40;&#41;
********{
************//call existing onload function
************existing&#40;&#41;;
************
************//call generic onload function
************generic&#40;&#41;;
********};
****}
****else
****{
********//setup onload function
********window.onload = generic;
****}
}

i simply swapped the alert code for my function.

here&#39;s how my page now looks:

&#60;&#33;DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http&#58;//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&#62;
&#60;html xmlns=&#34;http&#58;//www.w3.org/1999/xhtml&#34;&#62;
&#60;head&#62;
&#60;title&#62;Dynamic Clock&#60;/title&#62;
&#60;meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=iso-8859-1&#34; /&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;&#33;-- this would be a plugin --&#62;
&#60;img src=&#34;cb.gif&#34; name=&#34;a&#34;&#62;&#60;img src=&#34;cb.gif&#34; name=&#34;b&#34;&#62;
&#60;img src=&#34;colon.gif&#34; name=&#34;c&#34;&#62;&#60;img src=&#34;cb.gif&#34; name=&#34;d&#34;&#62;
&#60;img src=&#34;cb.gif&#34; name=&#34;e&#34;&#62;&#60;img src=&#34;colon.gif&#34; name=&#34;f&#34;&#62;
&#60;img src=&#34;cb.gif&#34; name=&#34;g&#34;&#62;&#60;img src=&#34;cb.gif&#34; name=&#34;h&#34;&#62;
&#60;img src=&#34;cam.gif&#34; name=&#34;j&#34;&#62;
&#60;script type=&#34;text/javascript&#34; language=&#34;javascript&#34; src=&#34;clock.js&#34;&#62;&#60;/script&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;onload.js&#34;&#62;&#60;/script&#62;
&#60;&#33;-- end of plugin --&#62;
&#60;/body&#62;
&#60;/html&#62;

Well, you get the drift.

Great stuff, hope it helps someone...

72dpi
07-11-2006, 09:14 AM
Or this method looks quite nice:
http://simon.incutio.com/archive/2004/05/26/addLoadEvent


try this method:

&#60;&#33;DOCTYPE HTML PUBLIC &#34;-//W3C//DTD HTML 4.01//EN&#34; &#34;http&#58;//www.w3.org/TR/html4/strict.dtd&#34;&#62;
&#60;html&#62;
&#60;head&#62;
&#60;meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=utf-8&#34;&#62;
&#60;title&#62;addLoadEvent&#40;&#41; test&#60;/title&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;p&#62;Here goes...&#60;/p&#62;
&#60;script type=&#34;text/javascript&#34;&#62;
function addLoadEvent&#40;func&#41; {
****var oldonload = window.onload;
****if &#40;typeof window.onload &#33;= &#39;function&#39;&#41; {
********window.onload = func;
****} else {
********window.onload = function&#40;&#41; {
************if &#40;oldonload&#41; {
****************oldonload&#40;&#41;;
************}
************func&#40;&#41;;
********}
****}
}

addLoadEvent&#40;function&#40;&#41; {
****document.body.style.backgroundColor = &#39;yellow&#39;;
}&#41;
addLoadEvent&#40;function&#40;&#41; {
****document.body.appendChild&#40;document.createTextN ode&#40;&#39;Hungry&#39;&#41;&#41;;;
}&#41;
addLoadEvent&#40;function&#40;&#41; {
****document.body.appendChild&#40;document.createTextN ode&#40;&#39; monkeys&#33;&#39;&#41;&#41;;;
}&#41;
&#60;/script&#62;
&#60;/body&#62;
&#60;/html&#62;

I am gonna implement these kinda functions when my scripts normally rely on Onload.

Any suggestions are welcomed.