Jquery "contains" code does not work on chrome

I use this code to check which language is on the site and then remove it from the drop-down menu. The code works in Firefox, but does not work on chrome and stops all other scripts. This is the code:

var mylangme = $(location).attr('href'); if(mylangme.contains("/fr/")){ mylangme="French"; $(".subnav li:first-child").css("display","none"); } if(mylangme.contains("/nl/")){ mylangme="Dutch"; $(".subnav li:nth-of-type(2)").css("display","none"); } if(mylangme.contains("/ru/")){ mylangme="Russian"; $(".subnav li:nth-of-type(3)").css("display","none"); } if(mylangme.contains("/en/")){ mylangme="English"; $(".subnav li:last-child").css("display","none"); } 
+8
javascript jquery
source share
2 answers

@Quentin is right, you are using the jQuery method for an object other than jQuery. You can fix this by using the indexOf method, which is part of the standard JavaScript library, and thus is supported by all browsers. The indexOf method returns -1 if the row is not found. Then your code will look like this:

 if(mylangme.indexOf("/fr/") != -1) { mylangme="French"; $(".subnav li:first-child").css("display","none"); } 
+14
source share

This is not jQuery. The attr method returns a string, which is the main JavaScript.

The contains method for strings was introduced in JavaScript 1.9 and is only supported by Firefox.

Use indexOf or polyfill listed on the MDN documentation page (above link).

+8
source share

All Articles