0

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";
  }
3
  • 2
    What doesnt work? Do you get an error message ? Commented Apr 5, 2015 at 16:03
  • 1
    You have st1 and st2 but you are using str1 and str2 ... was that a typo? Commented Apr 5, 2015 at 16:06
  • Because the document.write(tosearch) for example doesn't write enything. Commented Apr 5, 2015 at 16:06

1 Answer 1

1

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. :)

Sign up to request clarification or add additional context in comments.

4 Comments

I do like this str1 and str2 because otherwise the script would have foundd itself (the word not available). This is why i split it in 2 parts.
I am atrying to find if page contains "Not available"
FYI you could also declare your string as "Not\ available" if you want to avoid finding a self-match.
Thanks. It is much shorter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.