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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...