How can I check if one string contains another substring in Javascript?
Usually I would expect a String.contains()
method, but there doesn't seem to be one.
Edit : thanks for all the answers :) however it seems that I have another problem :(
when I use the ".indexof" method, Firefox refuses to start the javascript (this is for an extension).
My code is :
var allLinks = content.document.getElementsByTagName("a");
for (var i=0, il=allLinks.length; i<il; i++) {
elm = allLinks[i];
var test = elm.getAttribute("class");
if (test.indexof("title")!=-1) {
alert(elm);
foundLinks++;
}
}
if (foundLinks === 0) {
alert("No title class found");
}
else {
alert("Found " + foundLinks + " title class");
}
Firefox doesn't display an alert box. This works if I get rid of the .indexof() method. I already tried something like if(test=="title")... but it didn't work.