user2535974
user2535974

Reputation: 83

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");
                    }   

Upvotes: 8

Views: 6042

Answers (2)

Erik Schierboom
Erik Schierboom

Reputation: 16636

@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");
}

Upvotes: 14

Quentin
Quentin

Reputation: 943561

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.

Upvotes: 8

Related Questions