ArticleZip > Using Window Location Hash Includes Throws Object Doesnt Support Property Or Method Includes In Ie11

Using Window Location Hash Includes Throws Object Doesnt Support Property Or Method Includes In Ie11

When handling code that involves the Window Location Hash `includes` method in Internet Explorer 11 (IE11), you may encounter an error stating "Object doesn't support property or method 'includes'." This issue arises because IE11 does not natively support the `includes` method for checking if an array contains a certain value. However, fear not! There are workarounds to help you resolve this problem and ensure your code runs smoothly across different browsers.

One simple approach to address this incompatibility in IE11 is to use the indexOf method as an alternative to `includes`. The `indexOf` method returns the first index at which a given element can be found in the array, or -1 if it is not present. By using `indexOf`, you can effectively check if a specific value is included in an array even in Internet Explorer.

Here’s an example demonstrating how you can modify your code to replace `includes` with `indexOf`:

Javascript

const myArray = ['apple', 'banana', 'orange'];
const searchItem = 'banana';

if (myArray.indexOf(searchItem) !== -1) {
  console.log(`"${searchItem}" is included in the array.`);
} else {
  console.log(`"${searchItem}" is not found in the array.`);
}

In this snippet, we first define an array `myArray` and the value we want to search for, 'banana'. We then use the `indexOf` method to check if 'banana' exists in `myArray`. If the `indexOf` method returns a value other than -1, it means the item exists in the array.

By replacing `includes` with `indexOf` in your code, you can ensure cross-browser compatibility, including support for IE11. This simple adjustment can save you from encountering the 'Object doesn't support property or method 'includes'' error in IE11.

Another method to overcome the lack of `includes` support in IE11 is to use a polyfill. Polyfills are code snippets that emulate the standard behavior of modern JavaScript features in older browsers. You can include a polyfill for the `includes` method at the beginning of your script to provide the necessary support for IE11.

One popular polyfill library is Babel, which offers a broad range of polyfills for various JavaScript methods and functions. By including the Babel polyfill library in your project, you can ensure consistent behavior across different browsers, including IE11.

In conclusion, when encountering the 'Object doesn't support property or method 'includes'' error in IE11, consider using `indexOf` as an alternative or incorporating a polyfill like Babel to provide the necessary support. These simple solutions will help you enhance the compatibility of your code and deliver a seamless experience for users across different browser environments.

×