Jump to content
tinfanide

Object.prototype In Ie9-

Recommended Posts

<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-?

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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().

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Yes, that's where I've been staggering on.

but this works on IE8
And when I put the codes refined
HTMLCollection.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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Oh, I see.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...