Trouvé sur Tout JavaScript.com
Moteur de recherche qui surligne les mots trouvés dans une page. Peut-être pratique pour trouver des mots dans une page assez longue.
La fonction Rechercher du navigateur est a peu près équivalente à ce script. Le script affiche en plus le nombre de fois que le mot a été trouvé dans la page.
Fonctionne avec IE4, IE5 et IE6, avec Netscape 4, 6 et 7, avec Mozilla 1.X.
<head> <script language="JavaScript"> var nbSearch=0; function findInPage(str) { var txt, i, found; if (str=="") return false; if ((document.layers)||(window.sidebar)) { if (!window.find(str)) { alert("Fin de page atteinte.\n"+'"'+str+'" trouvé '+nbSearch+" fois."); while(window.find(str, false, true)) {nbSearch++;} } else nbSearch++; if (nbSearch == 0) alert('"'+str+'" est introuvable'); } if (document.all) { txt = window.document.body.createTextRange(); for (i = 0; i <= nbSearch && (found = txt.findText(str)) != false; i++) { txt.moveStart("character", 1); txt.moveEnd("textedit"); } if (found) { txt.moveStart("character", -1); txt.findText(str); txt.select(); txt.scrollIntoView(); nbSearch++; } else { if (nbSearch > 0) { alert("Fin de page atteinte.\n"+'"'+str+'" trouvé '+nbSearch+" fois."); nbSearch = 0; findInPage(str); } else { alert('"'+str+'" est introuvable'); } } } return false; } </script> </head> <Body> <form name="search" onSubmit="return findInPage(this.motcle.value);"> |