Foros del Web » Programando para Internet » Javascript »

Buscador de palabras interno, Resaltar texto encontrado.(No funciona en firefox)

Estas en el tema de Buscador de palabras interno, Resaltar texto encontrado.(No funciona en firefox) en el foro de Javascript en Foros del Web. Amigos, como puedo lograr que este buscador, me resalte el texto encontrado, que lo pinte de amarillo, cuando lo encuentre, ya que solo lo selecciona, ...
  #1 (permalink)  
Antiguo 19/08/2012, 14:20
 
Fecha de Ingreso: agosto-2008
Mensajes: 48
Antigüedad: 15 años, 8 meses
Puntos: 0
Buscador de palabras interno, Resaltar texto encontrado.(No funciona en firefox)

Amigos, como puedo lograr que este buscador, me resalte el texto encontrado, que lo pinte de amarillo, cuando lo encuentre, ya que solo lo selecciona, y a veces el resultado, no se peude localizar tan facil.

ADEMAS EL CODIGO, unicamente funciona en internet explorer, me gustaria saber si alguien conoce otro codigo que haga lo mismo, que funcione en firefox tambien, segun he revisado el codigo ya es algo antiguo.

Gracias! ojala alguien se anime y me ayude.

Código Javascript:
Ver original
  1. <script language="JavaScript">
  2. <!-- More javascripts http://www.hypergurl.com -->
  3.  
  4. var NS4 = (document.layers);    // Which browser?
  5. var IE4 = (document.all);
  6.  
  7. var win = window;    // window to search.
  8. var n   = 0;
  9.  
  10. function findInPage(str) {
  11.  
  12.   var txt, i, found;
  13.  
  14.   if (str == "")
  15.     return false;
  16.  
  17.   // Find next occurance of the given string on the page, wrap around to the
  18.   // start of the page if necessary.
  19.  
  20.   if (NS4) {
  21.  
  22.     // Look for match starting at the current point. If not found, rewind
  23.     // back to the first match.
  24.  
  25.     if (!win.find(str))
  26.       while(win.find(str, false, true))
  27.         n++;
  28.     else
  29.       n++;
  30.  
  31.     // If not found in either direction, give message.
  32.  
  33.     if (n == 0)
  34.       alert("Not found.");
  35.   }
  36.  
  37.   if (IE4) {
  38.     txt = win.document.body.createTextRange();
  39.  
  40.     // Find the nth match from the top of the page.
  41.  
  42.     for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
  43.       txt.moveStart("character", 1);
  44.       txt.moveEnd("textedit");
  45.     }
  46.  
  47.     // If found, mark it and scroll it into view.
  48.  
  49.     if (found) {
  50.       txt.moveStart("character", -1);
  51.       txt.findText(str);
  52.       txt.select();
  53.       txt.scrollIntoView();
  54.       n++;
  55.     }
  56.  
  57.     // Otherwise, start over at the top of the page and find first match.
  58.  
  59.     else {
  60.       if (n > 0) {
  61.         n = 0;
  62.         findInPage(str);
  63.       }
  64.  
  65.       // Not found anywhere, give message.
  66.  
  67.       else
  68.         alert("Not found.");
  69.     }
  70.   }
  71.  
  72.   return false;
  73. }
  74.  
  75. </script>
  76.  
  77.  
  78.  
  79. <body bgcolor="#FFFFFF" text="#000000">
  80. <p>
  81.  
  82.  
  83. <form name="search" onSubmit="return findInPage(this.string.value);">
  84. <input name="string" id="buscador" type="text" size=15 onChange="n = 0;">
  85.  <input type="submit" value="Search Page"></form>
  86.  
  87.  
  88. <span id="aa"> </span></p>
  89. <p>JScript es un lenguaje <strong>de <em>secuencias</em></strong> de comandos<em>
  90. interpretado</em> y basado en objetos. Aunque tiene menos funciones que los
  91. lenguajes orientados a objetos de altas prestaciones como C++, JScript es muy
  92. eficiente para los prop&oacute;sitos<font color="#FF0000" face="Arial, Helvetica, sans-serif"> a los que se destina.</font>
  93. </p>
  94. <p></p>

Etiquetas: funcion, input, interno, js, palabras, resaltar, select, buscadores
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:22.