Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript runtime error: Object doesn't support property or method 'contains'

Tags:

javascript

I have written the below code in Master Page

 <script type="text/javascript">
            if (window.location.pathname.contains('Page1.aspx')) {
                var js = document.createElement("script");

                js.type = "text/javascript";
                js.src = 'http://code.jquery.com/jquery-1.8.3.js';


            }
            if (window.location.pathname.contains('Page2.aspx')) {
                var js = document.createElement("script");

                js.type = "text/javascript";
                js.src = 'http://code.jquery.com/jquery-1.9.1.js';


            }    

        </script>

In asp.net application, I have three pages page1, page2,page3 which are inherited by the same Master Page. When I am trying to access the page3, it is displaying error: "JavaScript runtime error: Object doesn't support property or method 'contains'"

like image 940
Billy Avatar asked Jan 12 '23 02:01

Billy


1 Answers

Unless you have defined it yourself (which you haven't), there's no such thing as contains() for strings1. Use includes(), which is practically the same thing.

Example:

console.log('abcdefg'.includes('def')); // true
console.log('abcdefg'.includes('ghi')); // false

If you have to support older browsers (like Internet Explorer), use indexOf():

console.log('abcdefg'.indexOf('def') !== -1); // true
console.log('abcdefg'.indexOf('ghi') !== -1); // false

Anyway, it is a horrible idea to use two different versions of jQuery on two pages - avoid it where possible.


1: Technically this isn't 100% true: at one point there was a built-in contains() function, but it was renamed to includes() for compatibility reasons.

like image 91
Qantas 94 Heavy Avatar answered Jan 17 '23 15:01

Qantas 94 Heavy