Home > Web Front-end > JS Tutorial > body text

Why is \'Object doesn\'t support property or method \'forEach\'\' Thrown for DOM Collections in Microsoft Browsers?

Susan Sarandon
Release: 2024-10-20 06:43:29
Original
199 people have browsed it

Why is

Issues with forEach() in Microsoft Browsers for DOM Collections

As developers, we often rely on the forEach() method to traverse and manipulate DOM collections. However, we may encounter the frustrating error "Object doesn't support property or method 'forEach'" in Internet Explorer 11 and Edge.

Understanding DOM Collections

To resolve this issue, we must understand that DOM methods like querySelectorAll return collections, namely NodeList or HTMLCollection, which do not inherently support forEach().

  • NodeList: Represents a snapshot of matching elements, does not automatically reflect DOM changes.
  • HTMLCollection: Similar to NodeList but live, reflecting changes in the DOM.

Polyfilling forEach()

Since NodeList is specified to have forEach(), we can safely polyfill it:

<code class="js">if (typeof NodeList !== "undefined" &amp;&amp; NodeList.prototype &amp;&amp; !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = Array.prototype.forEach;
}</code>
Copy after login

Ensuring Iterability

NodeList and HTMLCollection are iterable, but older browsers may not implement this. We can polyfill iterability as well:

<code class="js">if (typeof Symbol !== "undefined" &amp;&amp; Symbol.iterator &amp;&amp; typeof NodeList !== "undefined" &amp;&amp; NodeList.prototype &amp;&amp; !NodeList.prototype[Symbol.iterator]) {
    Object.defineProperty(NodeList.prototype, Symbol.iterator, {
        value: Array.prototype[Symbol.iterator],
        writable: true,
        configurable: true
    });
}</code>
Copy after login

With these polyfills in place, we can successfully use forEach() on NodeList and HTMLCollection even in IE11 and Edge.

Example Usage

<code class="js">var color_btns = document.querySelectorAll('#color > p');
color_btns.forEach(function(color) {
    // Our custom logic here...
});</code>
Copy after login

Additional Considerations

Some legacy DOM libraries may be confused by adding forEach() to HTMLCollection. In such cases, only polyfill forEach() on NodeList.

The above is the detailed content of Why is \'Object doesn\'t support property or method \'forEach\'\' Thrown for DOM Collections in Microsoft Browsers?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!