Reputation: 11
I am extremely new to coding (more specifically javascript) and only really know the very basics. I was wondering if there was an easy way to perhaps give an alert when a certain word or words are displayed on a webpage, for example if i was in an online chat room and someone wrote "noot noot" i could then get an alert.
Upvotes: 0
Views: 550
Reputation: 6254
To search for a specific word in given area you can use the JS function search()
which returns a value which being greater than 0 means it exists.
If you want to search the whole body of the webpage for example you can use:
document.getElementsByTagName('body')[0].innerHTML.search("W3Schools");
Or of a specific div with id output then you can use:
document.getElementById('output').innerHTML.search("W3Schools");
See the example below:
var exists = document.getElementById('output').innerHTML.search("root");
if(exists > 0)
{
alert("root exissts");
}
else
{
alert("root does not exist");
}
<div id="output">
<p>Hello I am root</p><br/>
<span>this is the root</span>
</div>
Upvotes: 1