SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I make prototypes work for me?

    I have a function :

    Code:
    Object.prototype.newEvent = function(event, func) {
    	if(this.addEventListener) {
    		this.addEventListener(event, func, false);
    		return true;
    	} else
    	if(this.attachEvent) {
    		this["e" + event + func] = func;
    		this[event + func] = function() {
    			this["e" + event + func](window.event);
    		}
    		this.attachEvent("on" + event, this[event + func] );
    	}
    }
    And I call it with :

    Code:
    				
    document.getElementById("TextBox").newEvent("focus", function() {
    	this.style.background = "white";
    	this.style.display = "none";
    });
    Why does this work in Firefox, but not IE.

    More importantly, how can I do this in IE?
    Last edited by jtillwick; Jul 1, 2006 at 21:56.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not having trouble with the event, it is the javascript prototype that is causing the error in IE. The event function works fine.
    Last edited by jtillwick; Jul 1, 2006 at 21:57.

  4. #4
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML elements in IE don't inherit from the base Object type, so you can't add new methods to them using Object.prototype. You can fix it with an .htc behaviour file, or just write your addEvent as a normal function.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •