Let's say the String "Done successfuly" is displayed somewhere in a webpage , is there a way to know if this string exist using javascript only ? Thanks.
More details would help. There may be better ways. But this is the best given what information you've provided:
if (
(
document.documentElement.textContent || document.documentElement.innerText
).indexOf('Done successfuly') > -1
) {
// Do something...
}
If you know the ID or class-name or can otherwise select the element that directly contains the text (e.g. h1#someHeading span ...
) then you should probably do that as it will be more performant and generally a cleaner approach.
EDIT Worth noting: This approach will pick up text within SCRIPT and STYLE tags too, which is probably not what you want. Both textContent
and innerText
have various quirks across browsers too. See this for more info.
Try .search() Something like this:
document.body.innerHTML.search("Done successfully!");
Or instead of innerHTML, use textContent
The easiest way of doing this is using jquery's contains selector:
if($("*:contains('Hello World')").length > 0)
console.log('yeah baby');
This way also gives you the container element of the text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With