Question : Trigger destroys previous trigger

I use the following JavaScript/Prototype code to trigger an event when a tab is changed:
1:
2:
3:
window.ontabschanged = function() {
alert('Tab Changed');
}

 The problem is that it destroys the previous trigger for when the tab is changed.
 
 For example, in the code below, only the third task is run.  The first two tasks are destroyed.
1:
2:
3:
4:
5:
6:
7:
8:
9:
window.ontabschanged = function() {
alert('First Task');
}
window.ontabschanged = function() {
alert('Second Task');
}
window.ontabschanged = function() {
alert('Third Task');
}

Answer : Trigger destroys previous trigger

Sorry, just need to change the event name to yours:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
<script type="text/javascript">
	var addToEvent = function(method) {
		var cachedEvt = window.ontabschanged;
		if (typeof cachedEvt !== 'function')
			window.ontabschanged= method;
		else {
			window.ontabschanged= function() {
				if (cachedEvt)
					cachedEvt();
				method();
			}
		}	
	};

	addToEvent(function() {
		alert('First Task');
	});
	
	addToEvent(function() {
		alert('Second Task');
	});
	
	addToEvent(function() {
		alert('Third Task');
	});
</script>
Random Solutions  
 
programming4us programming4us