Thank you DaveyErwin for a great link! I have been studing and maybe I got it right now. But what did you meant was messed up here:
for(var i = 0; i < els.length;i++){
els[i].id.indexOf("show") == 0 && divs.push(els[i]);
els[i].id.indexOf("link") == 0 && addEvent(els[i],"click", function(){
It is an anonymous function. In the browsers I have testet it seams now to work in every browser (I hope):
Code:
function addListeners(){
var els = document.getElementsByTagName('*');
divs = [];
for(var i = 0; i < els.length;i++){
els[i].id.indexOf("show") == 0 && divs.push(els[i]);
els[i].id.indexOf("link") == 0 && addEvent(els[i],"click", function(){
for (var i = 0; i < divs.length;i++){
divs[i].style.display = 'none'
}
document.getElementById('show' + this.id.split('link')[1]).style.display = 'block';
});
}
}
window.addEvent(window,"load", addListeners, false);
function addEvent( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
} else
obj.addEventListener( type, fn, false );
}
Even the code seam to work perfectly I don´t understand how function addEvent is fired off? When the window load and a click has happen the anonymous function show an element. Is it the loading of the window that make the function addEvent work?
I would appreciate if you would like to axplain the function for my own understanding.