HTMLElement doesnt work in IE
-
Wednesday, June 15, 2011 11:47 AM
HTMLElement dosent work in IE(im using v8) but it works in other browsers.
Can I use it somehow else?
All Replies
-
Friday, June 17, 2011 10:07 AMModerator
Hi ido,
Thanks for your post.
Do you have a URL of a minimal but complete enough document to demonstrate the problem?
Thanks.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework -
Friday, June 17, 2011 10:22 AM
No I dont have a url. but I have a code for example
<div><div onclick="divclick(this)">abcd</div</div> <script language="JavaScript"> HTMLElement.prototype.getInnerHTML = function(){return this.innerHTML;}; divclick(sender) { alert(sender.getInnerHTML()); } </script>
the error is that HTMLElement is undefined in line 3.
thank you for answering me,
Ido Sorozon
- Edited by ido sorozon Friday, June 17, 2011 10:22 AM innerHtml -> innerHTML
-
Tuesday, June 21, 2011 8:29 AMModerator
Hi ido,
thanks for your response.
In browsers that support adding methods to prototype of native objects such as HTMLElement, however, it is undefined in IE browser, here is a simple sample demonstrates how to extend function in IE, for your refer:
<html>
<head>
<script type="text/javascript">var DOMElement =
{
extend: function (name, fn) {
if (!document.all)
eval("HTMLElement.prototype." + name + " = fn");
else {
//
// IE doesn't allow access to HTMLElement
// so we need to override
// *document.createElement
// *document.getElementById
// *document.getElementsByTagName
////take a copy of
//document.createElement
var _createElement = document.createElement;//override document.createElement
document.createElement = function (tag) {
var _elem = _createElement(tag);
eval("_elem." + name + " = fn");
return _elem;
}//take copy of
//document.getElementById
var _getElementById = document.getElementById;//override document.getElementById
document.getElementById = function (id) {
var _elem = _getElementById(id);
eval("_elem." + name + " = fn");
return _elem;
}//take copy of
//document.getElementsByTagName
var _getElementsByTagName = document.getElementsByTagName;//override document.getElementsByTagName
document.getElementsByTagName = function (tag) {
var _arr = _getElementsByTagName(tag);
for (var _elem = 0; _elem < _arr.length; _elem++)
eval("_arr[_elem]." + name + " = fn");
return _arr;
}
}
}
};//DOMElement.extend("foo", function () { alert('bar') });
//DOMElement.extend("about", "DOMElement v0.1")
DOMElement.extend("contents", function () { return this.innerHTML })
// var elem = document.createElement("div");
// elem.foo();onload = function () {
var divs = document.getElementById("div1");
alert(divs.contents())
}</script>
</head>
<body>
<div id="div1">
abcd</div>
</body>
</html>
hope this helps, thanks.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
- Marked As Answer by Yanping WangMicrosoft Contingent Staff, Moderator Thursday, June 23, 2011 9:22 AM
-
Thursday, June 23, 2011 9:49 AM
with this method I need to call all the elements, that I want to use the function on them, in the page. so if I want to call the parentElement(or parentNode) I cant use this function.
But I found that in html 5 'Element' works (but not HTMLElement, I can write if(!HTMLElement){var HTMLElement = Element;} so it will work in all browsers) its good because I was planning to transfer to html 5.
thank you!
Ido sorozon
- Marked As Answer by ido sorozon Thursday, June 23, 2011 9:49 AM


