-6

I want to know how to check the web browser to see if a specific string of text exists.

For example, I want to validate that the string "Hello World" is visible on the browser. Any tips would be great. Thanks.

Ted
  • 14,757
  • 2
  • 41
  • 58
V Kid
  • 117
  • 4
  • 12
  • 2
    What have you tried? Where is this string supposed to be? On the webpage? In the URI? In a bookmark? Do you mean `visible` as in it's current CSS allows it to be physically seen on the screen? – Scott Apr 20 '15 at 18:35
  • can you try $(document).text().contains('Hello World') ? – Sushil Apr 20 '15 at 18:35
  • This needs to work for IE. @Sushil, that doesn't work for IE. – V Kid Apr 20 '15 at 18:38
  • @Sushil that's not what the OP is asking for, and your code is also wrong: there's no such thing as `.contains` for strings in Javascript. – Marco Bonelli Apr 20 '15 at 18:38
  • @ScottKaye I just want to verify that a "sfldjsdjfk" is visible anywhere on the browser. – V Kid Apr 20 '15 at 18:38
  • 1
    Does `document.getElementsByTagName('body')[0].innerHTML.indexOf('Hello World')` accomplish what you want?? – bloodyKnuckles Apr 20 '15 at 18:39
  • @bloodyKnuckles, not that does not. – V Kid Apr 20 '15 at 18:40
  • You're going to have to be a lot more clear with what you're asking - @bloodyKnuckles's solution will return the location (index) of "Hello World" if it exists anywhere in the HTML of the page, or `-1` if it isn't found. – Scott Apr 20 '15 at 18:42
  • 1
    @bloodyKnuckles You could also grab the body quicker with `document.body` – Scott Apr 20 '15 at 18:43
  • @MarcoBonelli the code i've given is in jquery and it does have a .contains property. you can just open your console window in chrome for this very page and run the code. it will work. – Sushil Apr 20 '15 at 18:45
  • @VKid I've tried this in IE 11. which version are you running? – Sushil Apr 20 '15 at 18:47
  • @Sushil console says object don't support property or method 'contain' using IE11 – V Kid Apr 20 '15 at 18:49
  • are you trying 'contain' or contains? which version of jquery are you running? can you share what you're trying? – Sushil Apr 20 '15 at 18:50
  • both. contain and contains – V Kid Apr 20 '15 at 18:56
  • can you share your code? it is difficult for us to assume what you're writing. – Sushil Apr 20 '15 at 18:58
  • 1
    @Sushil the [jQuery `.text()` method](https://api.jquery.com/text/) returns a **string**, and strings do not have a `.contains()` method. Check the documentation and run it on your console before claiming that it's correct, because it's not. – Marco Bonelli Apr 20 '15 at 19:09
  • @MarcoBonelli. I ran it in my console before posting it. – Sushil Apr 20 '15 at 19:11
  • @Sushil That's just impossible dude. Create a [JSFiddle](http://jsfiddle.net) and link it to me, I'm really courious. – Marco Bonelli Apr 20 '15 at 19:15
  • @MarcoBonelli you can see this link. i've just taken a screenshot http://postimg.org/image/wepviivyd/ – Sushil Apr 20 '15 at 19:19
  • @Sushil Yeah, that'a because Stack Overflow has another library which adds the `.contains` method to the `String.prototype`, but try running it on another site (e.g. Google), and you'll see that it's wrong. – Marco Bonelli Apr 20 '15 at 19:33
  • oh @MarcoBonelli. then I maybe wrong. I just tried it on this site. thanks for the info. – Sushil Apr 20 '15 at 19:36

1 Answers1

0

This should do the trick for you...

if($('html:contains("Hello World")').length > 0){
   alert('Hello World! I exist!');
}else{
   alert('Goodbye cruel world!')
}
Ted
  • 14,757
  • 2
  • 41
  • 58