Web Analytics Made Easy -
StatCounter How to add encapsulated event listeners in Mac/IE5? - CodingForum

Announcement

Collapse
No announcement yet.

How to add encapsulated event listeners in Mac/IE5?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to add encapsulated event listeners in Mac/IE5?

    (With apologies for the deja-vous )

    Mac/IE5 doesn't support the addition of event handlers using addEventListener or attachEvent, so is there any way of binding an encapsulated load handler, anything other than window.onload?

    Here's what I've currently got. The problem with it is that the final group of browsers are only sensitive to exisiting onload functions if they come before this one:
    Code:
    //identify konqueror and opera 7
    var kde = (navigator.vendor == 'KDE');
    var op7 = /opera[\/ ]7/i.test(navigator.userAgent);
    
    //.. gecko, safari and generic
    //need to exclude konqueror specifically
    //because it returns a function for window.addEventListener
    //but window.addEventListener('load' doesn't work
    if(!kde && typeof window.addEventListener != "undefined")
    {
    	//setup window listener to call initialising function
    	window.addEventListener('load',onloadFunction,false);
    }
    
    //.. opera 7
    else if(op7)
    {
    	//setup document listener to call initialising function
    	document.addEventListener('load',onloadFunction,false);
    }
    
    //.. windows IE
    else if(typeof window.attachEvent != "undefined")
    {
    	//setup window handler to call initialising function
    	window.attachEvent('onload',onloadFunction);
    }
    
    //.. mac/ie, osx/msn and konqueror
    //or anything else that gets this far
    else
    {
    	//if there's an existing onload function
    	if(typeof window.onload == "function")
    	{
    		//store it
    		var currentOnload = onload;
    		
    		//setup handler
    		window.onload = function()
    		{
    			//call it
    			currentOnload();
    			
    			//call initialising function
    			onloadFunction();
    		};
    	}
    	else
    	{
    		//setup handler to call initialising function
    		window.onload = onloadFunction;
    	}
    }
    
    
    
    //onload function
    function onloadFunction()
    {
    	alert("onload");
    }
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #2
    Of course, you already know about my registerEvent experiment, but it does something of the kind.
    liorean <[[email protected]]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

    Comment


    • #3
      Yeah I remember - but aren't you getting an existing load event in fundamentally the same way, such that if it came *after* your script it wouldn't be registered?
      "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

      Comment


      • #4
        Welll, yes. I can't affect that. However, my registerEvent function uses addEventListener in cases it can be used in browsers you throw to the DOM0 way, and it adds both DOM0 binding of this and the most commonly used [DOM2Events] Event object properties.

        It's still not entirely safe, however. I have not yet fixed bugs in it that affect at least saf and iem, though I think the op7.5 bugs are fixed at least.

        I'm wondering about whether extending this to a more complete API, including among other things an event remover would be a good idea...
        liorean <[[email protected]]>
        Articles: RegEx evolt wsabstract , Named Arguments
        Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
        Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

        Comment

        Working...
        X