Reputation: 11
I'm running IE 11 on my machine. I have a webpage that uses getElementsByClassName. When I access the webpage on my machine the page is served up and it works fine (it executes getElementsByClassName). If I access the same webpage on a different machine also using IE and it can’t find the method getElementsByClassName. If I save the code served up to the browser on the failing machine and access that, the saved page works fine (it executes elemmentsByClassName works). What is going on? How do I tell the browser to use the version of the DOM or javascript that has getElementsByClassName?
var list = document.getElementsByClassName("mrClickableRow");
Upvotes: 1
Views: 851
Reputation: 5387
For compatibility issues, you can use this function.
window.getElementsByClassName = function(node, classname) {
var a = [];
var re = new RegExp('(^| )' + classname + '( |$)');
var els = node.getElementsByTagName("*");
for (var i = 0, j = els.length; i < j; i++)
if (re.test(els[i].className)) a.push(els[i]);
return a;
}
if (!document.getElementsByClassName) {
document.getElementsByClassName = function(className) {
return window.getElementsByClassName(document.body.parentNode, className);
};
}
But be aware that this function and the built-in function are slightly different: When you use the built-in document.getElementsByClassName
function, it returns an HTMLCollection
. If you then delete the class of an element contained in the HTMLCollection, it is also removed from the HTMLCollection.
Upvotes: 0
Reputation: 379
Make sure that IE is using the correct Document Mode to render your page, if not, it can simulate and older DOM-API which can cause some problems.
Open your Developer Tools by hitting F12 in IE, or selecting it by the settings-menu. Then go to the Emulation-tab and look at the Document Mode-dropdown.
Upvotes: 2
Reputation: 504
document.getElementsByClassName
is available from IE 9, are you sure you integrate JS with <script type="text/javascript">
?
P.S. Use <script>
before the </body>
(body closing tag)
Upvotes: 0