Jump to content

How do I search content of current page?


Guest Will

Recommended Posts

I made up this little script. It needs the innerText property in order to be cross-browser. Unfortunately, that makes it non-W3C-compliant. But until some browsers improve their compliance to web standards it will always be hard to make standard W3C compliant scripts.

function searchPage(obj) {   if(document.documentElement.textContent) {	  allText = document.body.textContent;   } else {	  allText = document.body.innerText;   }   if(allText.indexOf(obj) != -1) {	  //Execute some code   }}

Search: <input type="text" id="something" /><input type="button" value="Search" onclick="searchPage(document.getElementById('something').value)" />

Link to comment
Share on other sites

Because innerHTML will also return HTML tags. And imagine you search for "table", and it says it found it but you can't see the word "table" anywhere at all (supposing you're a visitor that doesn't know anything about HTML).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...