Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery "contains" code doesn't work on chrome

I'm using this code to check what language is on the site and then remove it from the my dropdown menu. The code works in Firefox but fails to work on chrome and stops all other scripts as well. 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");
                    }   
like image 1000
user2535974 Avatar asked Jun 30 '13 06:06

user2535974


2 Answers

@Quentin is right, you are using a jQuery method on a non-jQuery object. You can fix it using the indexOf method that is part of the standard JavaScript library, and as such is supported by all browsers. The indexOf method will return -1 if the string was not found. Your code would then look like this:

if(mylangme.indexOf("/fr/") != -1) {
    mylangme="French";
    $(".subnav li:first-child").css("display","none");
}
like image 190
Erik Schierboom Avatar answered Oct 07 '22 11:10

Erik Schierboom


That isn't jQuery. The attr method returns a String, which is core JavaScript.

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

Use indexOf or the polyfill given on the (above linked) MDN documentation page.

like image 23
Quentin Avatar answered Oct 07 '22 09:10

Quentin