Brana
Brana

Reputation: 1239

Javascript string search doesn't work

I need to check if a page contains some text. Here is the code that doesn't work, but i am not able to determine why:

  var st1 = "Not";
  var st2 = "available";
  var tosearch = str1.concat(str2);
  document.write(tosearch);
  var Availability = "not defined";
  if(document.body.innerHTML.toString().indexOf(tosearch) > -1){
       Availability = "yes";
  } else {
     Availability = "fdssssssssssssssssssssssss";
  }

Upvotes: 0

Views: 112

Answers (1)

bvaughn
bvaughn

Reputation: 13487

It's probably not working because you declare st1 and st2 but then reference them as str1 and str2. Change to...

  var str1 = "Not";
  var str2 = "available";
  var tosearch = str1.concat(str2);
  // The rest of your code...

You're also creating the string "Notavailable" which is not what you intend to, I think. Maybe try:

  var tosearch = str1 + " " + str2;

You could do the same thing with even less code like so:

if(document.body.innerHTML.toString().indexOf("Not\ available") > -1){
  alert("Yes")
} else {
  alert("No")
}

That being said, it's kind of a mystery to me what you're trying to do with this script. :)

Upvotes: 1

Related Questions