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");
}
@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");
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With