views:

29

answers:

1

Hello, i m a beginner but i need a script that could help me in some pages. I need a script that analazizes the text in the page and if there is the word that i m searching for it shows a popoup, if not does nothing. here s the code but it doesn t work with firefox because the text range function is only for IE (probably won t work either because i cant do javascript). Someone told me to use the tostrign function but i don't know how :P So if you could please change it so that it could work that would be really great.

  document.body.onload = cerca();
  function cerca() {
   Range = document.body.createRange();
   campo = toString();
   var c = campo.findText("ciao");
   if(c){
      alert("Corrispondenza Trovata") } else  {alert("nada"); } 
  }

thank you very much :)

A: 

Firefox has Ranges instead of TextRanges. If you just want to check for the presence of a particular piece of text in the page and not highlight it, the following will work in Firefox. The reason for using the Selection object is that a Range encompassing the whole body would includes all text nodes within the body, including those inside <script> elements, while the result of calling toString() on the Selection object only includes visible text, which is what you want. Note also that this function wipes out the current selection, if one exists; if this is a problem for you then you can store and later restore the selected ranges.

function visibleTextContains(str) {
    var range = document.createRange();
    range.selectNodeContents(document.body);
    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
    var visibleText = sel.toString();
    sel.removeAllRanges();
    return visibleText.indexOf(str) > -1;
}
Tim Down