0

I simply want to have an alert that says "Page contains string" if a specific string exists on a page.

Shouldn't it be as simple as this:

if ('*:contains("This String")') {
    alert('Page Contains String');
}
Posz
  • 83
  • 1
  • 7
  • 1
    Missed to add jQuery lib(usually a `dollar symbol`), `$('*:contains("This String")')` or `jQuery('*:contains("This String")')` – Praveen May 14 '14 at 12:26

4 Answers4

7

Native JS:

if(document.body.innerText.indexOf("This String") !== -1){
    // Do stuff
}

You might want to use document.body.textContent instead, though. Depending on what browsers you want / need to support. (textContent is not supported by IE 8 or lower.)

For maximum compatibility, try this:

var text = document.body.innerText || document.body.textContent;
if(text.indexOf("This String") !== -1){
    //Do stuff
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • You should use the '>' comparator instead of '!==', it will be faster. – Apolo May 14 '14 at 12:29
  • 3
    @Apolo: Did you really just downvote me for such a micro optimization? Then downvote the other 2 answers for using jQuery. – Cerbrus May 14 '14 at 12:30
  • Guys, really? 2 Downvotes? At least comment with a good reason on what's so bad about my answer. – Cerbrus May 14 '14 at 12:32
  • 5
    @Apolo talking about optimization but suggesting using jQuery.. (facepalm) – DontVoteMeDown May 14 '14 at 12:32
  • nope, as your edit mentionned, I would recommend using textContent which is supported by all recent navigator. Or this way : `txt = div.textContent || div.innerText || '';` – Apolo May 14 '14 at 12:32
  • @DontVoteMeDown he is asking for a JQuery solution (see tag). Also `$("body")` is not a huge loss of performance. – Apolo May 14 '14 at 12:34
  • 1
    @Apolo, nope.. the difference between `<` and `!==` that have huge loss performance. – DontVoteMeDown May 14 '14 at 12:35
  • 3
    @Apolo: He's just saying jQuery is an option. Also, `document.body` is [**so very much faster**](http://jsperf.com/jquery-body-vs-document-body-selector/26), it's not even anywhere near close. (It's **50** times as fast!) – Cerbrus May 14 '14 at 12:36
  • @Apolo: I added a compatibility option, so your downvote is no longer valid. Could you remove it please? \[edit] Thanks! [/edit] – Cerbrus May 14 '14 at 12:45
  • Done. Also, another good answer related : http://stackoverflow.com/a/1789952/3484498 See first comment. I add a pure JS solution based on that to my answer so downvote should be removed too I guess. – Apolo May 14 '14 at 12:47
2

Try this way :

if($("body").text().indexOf("This String") > -1) {
    alert("page contains string");
}

Or pure JS :

if (~document.body.textContent.indexOf('This String')) {
    alert("page contains string");
}
Apolo
  • 3,844
  • 1
  • 21
  • 51
  • 4
    Ooh, you downvoted me because you've also got an answer here. Instead of using `>`, try not to use jQuery. – Cerbrus May 14 '14 at 12:32
  • 2
    ... Always go for readable code. `~` is not developer-friendly. And did you seriously leave out the compatibility code you downvoted me for not having? – Cerbrus May 14 '14 at 12:51
2

create a string of the html

//Retrieves html string
var htmlString = $('body').html();

The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs.

var index = htmlString.indexOf("this string");

if (index != -1)
    alert("contains string");

One-Liner

if($('body').html().indexOf("this string") !== -1)
     alert("contains string")
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Aerox
  • 69
  • 3
2

Yet another method with jQuery -

if( $('body:contains(string)').length ) {
    console.log('found it');
};

Your syntax is a little messed up, but you were on the right path.

if ( $('*:contains("This String")').length ) { // you needed an alias to jQuery, some proper parentheses and you must test for length
    alert('Page Contains String');
}

testable here - http://jsfiddle.net/jayblanchard/zg75Z/

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119