Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples

Sponsored Links

Discuss "addLoadListener - adding onload event in generic way" in the Code Samples forum.

Code Samples - Search through our code samples to give your application that something extra or provide a code sample of your own.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old July 13th, 2008, 01:20 PM
Shadow Wizard's Avatar
Ask Me About Dragons :D

 
Join Date: Jul 2008
Posts: 23
Thanks: 0
Thanked 4 Times in 4 Posts
Blog Entries: 1
Rep Power: 1
Shadow Wizard is on a distinguished road

Awards Showcase
Microsoft .Net Classic ASP JavaScript 
Total Awards: 3

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)
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > 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
Forum Jump

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 01:52 PM.



Content Relevant URLs by vBSEO 3.2.0