Billy
Billy

Reputation: 905

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

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'"

Upvotes: 5

Views: 13479

Answers (1)

Qantas 94 Heavy
Qantas 94 Heavy

Reputation: 16020

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.

Upvotes: 10

Related Questions