tinfanide Posted January 14, 2012 Share Posted January 14, 2012 <body><div id="div" class="class0">class 0</div><div class="class1">class 1.0</div><div class="class1">class 1.1</div><div class="class3">class 3</div><span class="class1">class 1.2</span><script type="text/javascript" language="javascript">Object.prototype.getClassName = function(ClassName){ var x=-1; var elements = []; while(x<this.getElementsByTagName("*").length-1){ ++x; if(this.getElementsByTagName("*")[x].className==ClassName){ elements.push(this.getElementsByTagName("*")[x]); } } return elements; }alert(document.getClassName("class1")[1].innerHTML);</script></body> How can make Object.prototype work in IE9-? Link to comment Share on other sites More sharing options...
Ingolme Posted January 14, 2012 Share Posted January 14, 2012 I believe you have to add it to the prototype of the exact class that the object is instantiated from.Document.prototype.getClassName or maybe HTMLElement.prototype.getClassName Though I seem to remember that Internet Explorer didn't let me modify the HTMLElement prototype last time I tried. In the worst case, you'll have to make a global getClassName that accepts the elements as one of the parameters: getClassName(element, className) Link to comment Share on other sites More sharing options...
tinfanide Posted January 14, 2012 Author Share Posted January 14, 2012 Though I seem to remember that Internet Explorer didn't let me modify the HTMLElement prototype last time I tried. In the worst case, you'll have to make a global getClassName that accepts the elements as one of the parameters: getClassName(element, className) Yes, it works in the way suggested above. var getClassName = function(element,ClassName){ var x=-1; var elements = []; while(x<element.getElementsByTagName("*").length-1){ ++x; if(element.getElementsByTagName("*")[x].className==ClassName){ elements.push(element.getElementsByTagName("*")[x]); } } return elements;} // getClassName(element,ClassName)alert(getClassName(document,"class3")[0].innerHTML);/////////////////////// But I cannot understand what you mean here: I believe you have to add it to the prototype of the exact class that the object is instantiated from.Document.prototype.getClassName or maybe HTMLElement.prototype.getClassName HTMLElement.prototype.getClassName What is HTMLElement? Can you give a simple example? Link to comment Share on other sites More sharing options...
Ingolme Posted January 14, 2012 Share Posted January 14, 2012 HTMLElement is the browser's internal mechanism to create an element. An element is an HTMLElement() and not a plain Object(). Inside the browser, when the page is loaded, elements are created soemthing like this: document = new Document();document.documentElement = new HTMLElement(); They aren't just plain Object() but HTMLElement(), Document(), and other classes that extend from it, like Form() and Image(). Link to comment Share on other sites More sharing options...
tinfanide Posted January 14, 2012 Author Share Posted January 14, 2012 HTMLElement is the browser's internal mechanism to create an element. An element is an HTMLElement() and not a plain Object(). Inside the browser, when the page is loaded, elements are created soemthing like this:document = new Document();document.documentElement = new HTMLElement(); They aren't just plain Object() but HTMLElement(), Document(), and other classes that extend from it, like Form() and Image(). document = new Document();document.documentElement = new HTMLElement();HTMLElement.prototype.getClassName = function(ClassName){ var x=-1; var elements = []; while(x<this.getElementsByTagName("*").length-1){ ++x; if(this.getElementsByTagName("*")[x].className==ClassName){ elements.push(this.getElementsByTagName("*")[x]); } } return elements;}alert(document.getClassName("class1")[1].innerHTML); IE9 return error lines: SCRIPT445: Object doesn't support this actiongetElementsByClassName.html, line 49 character 1 var rootElement = document.documentElement; Link to comment Share on other sites More sharing options...
Ingolme Posted January 14, 2012 Share Posted January 14, 2012 Like I said earlier, Internet Explorer doesn't let you modify the prototype of the HTML elements. document = new Document();document.documentElement = new HTMLElement(); Also, what I told you earlier, this is what the browser does automatically, it's not part of the Javascript you write. Link to comment Share on other sites More sharing options...
tinfanide Posted January 14, 2012 Author Share Posted January 14, 2012 How about this example?http://stackoverflow.com/questions/7766673/ie-only-javascript-error-object-prototype-and-getelementsbytagname Link to comment Share on other sites More sharing options...
Ingolme Posted January 14, 2012 Share Posted January 14, 2012 Well, there you have an example. As the answer shows, these things hardly work in Internet Explorer.Quoting from the page you linked: You should basically not extend host objects, but this works on IE8 Link to comment Share on other sites More sharing options...
tinfanide Posted January 14, 2012 Author Share Posted January 14, 2012 Yes, that's where I've been staggering on. but this works on IE8And when I put the codes refinedHTMLCollection.prototype.size = function(){ return this.length;}; and try to see how IE9- (IE7, IE8) interacts with HTMLCollection buteven IE9 does not respond well to HTMLCollection...So I doubt if the writer was right or I should not play around with Object.prototype in IE, I'm just wondering... Link to comment Share on other sites More sharing options...
Ingolme Posted January 14, 2012 Share Posted January 14, 2012 You shouldn't try to extend HTML objects because they're not very reliable, especially in Internet Explorer. Internet Explorer seems to use HTMLCollection while other browser use NodeList. Link to comment Share on other sites More sharing options...
tinfanide Posted January 15, 2012 Author Share Posted January 15, 2012 Oh, I see. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.