DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

addLoadListener - adding onload event in generic way

This is a discussion on addLoadListener - adding onload event in generic way within the JavaScript Code Samples forums, part of the JavaScript Programming category; One of the most common problems with JavaScript is adding events to run in the page onload event, meaning after ...

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming > JavaScript Code Samples

  #1  
Old July 13th, 2008, 01:20 PM
Shadow Wizard's Avatar
Ask Me About Dragons :D
 
Join Date: Jul 2008
Posts: 54
Blog Entries: 2
Rep Power: 2
Shadow Wizard will become famous soon enough
Post addLoadListener - adding onload event in generic way

One of the most common problems with JavaScript is adding events to run
in the page onload event, meaning after the page finished loading.

Here is function that solve lots of headache:
Code:
<script type="text/javascript">
function addLoadListener(fn) {
	if (typeof window.addEventListener != "undefined")
	{
		window.addEventListener("load", fn, false);
	}
	else if (typeof document.addEventListener != "undefined")
	{
		document.addEventListener("load", fn, false);
	}
	else if (typeof window.attachEvent != "undefined")
	{
		window.attachEvent("onload", fn);
	}
	else {
		var oldfn=window.onload; if (typeof window.onload != "function") window.onload = fn; else window.onload = function() { oldfn(); fn();}
	}
}
</script>
how to use?
like this:
Code:
<script type="text/javascript">
function FirstFunc()
{
	alert("hello I'm first");
}

function SecondFunc()
{
	alert("hello I'm second");
}

addLoadListener(FirstFunc);
addLoadListener(SecondFunc);
</script>
this will activate both functions.. ordinary code would overwrite
the previous onload and trigger only the latest.

note: the order of execution might differ among different browsers.
for example in IE, it's LIFO (Last In First Out, meaning the second function
above will get executed before the first) and in Firefox it's FIFO - First In
First Out, meaning the first will be first and second is the second.
yes, I know you're not surprised.

Happy Programming!

Comments on this post
jmurrayhead agrees: nice
dr_rock agrees: LOL FIFO/FILO WHY OH WHY!! (sigh* -writes same function twice AGAIN-)
Reply With Quote
The Following User Says Thank You to Shadow Wizard For This Useful Post:
jmurrayhead (July 14th, 2008)
Reply

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming > JavaScript Code Samples

Bookmarks

Tags
listener, onload

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
On onchange event hide blank frame guddu JavaScript Programming 3 September 8th, 2008 09:14 AM
Generic Form Handler richyrich .Net Development 3 July 28th, 2008 01:34 PM
Generic Paging Class Shem .Net Development 4 July 18th, 2008 11:29 AM
Grouping buttons to one event Shem .Net Development 8 July 17th, 2008 06:53 AM
adding an event for my btnDelete in repeater Shem .Net Development 13 July 15th, 2008 09:33 AM


All times are GMT -4. The time now is 07:07 AM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2