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');
}
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');
}
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
}
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");
}
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")
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/