Ver Mensaje Individual
  #4 (permalink)  
Antiguo 06/10/2007, 10:12
escudo40
 
Fecha de Ingreso: enero-2007
Mensajes: 51
Antigüedad: 17 años, 3 meses
Puntos: 0
Re: Ayuda para pasar esto a una función...

a mi me funciona perfecto con este codigo

Código HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
<script src="js/funciones_varias.js" type="text/javascript"></script>
 </head>
 <body>
  <form name="frm">
   Texto:<br>
   <textarea name="txt" rows="5" cols="80" onKeyDown="calcula()"></textarea><br><br>
   Separadores:
   <input type="text" name="sep" value=" ,.;:">
   <input name="contador" type="text" id="contador" size="4" maxlength="4">
   <br>
   <br>
   
  </form>
 </body>
</html> 
el archivo de javascript se encuentra en la carpeta "js" y el nombre del archivo es "funciones_varias.js"
Código:
   function calcula(){
    var sTxt = document.frm.txt.value;
    var sTx2 = "";
    var sSep = document.frm.sep.value;
    var iRes = 0;
    var bPalabra = false;
    for (var j = 0; j < sTxt.length; j++){
     if (sSep.indexOf(sTxt.charAt(j)) != -1){
      if (bPalabra) sTx2 += " ";
      bPalabra = false;
     } else {
      bPalabra = true;
      sTx2 += sTxt.charAt(j);
     }
    }
    if (sTx2.charAt(sTx2.length - 1) != " ") sTx2 += " ";
    for (var j = 0; j < sTx2.length; j++)
     if (sTx2.charAt(j) == " ") iRes++;
    if (sTx2.length == 1) iRes = 0;
   //alert("Número de palabras: " + String(iRes));
   document.frm.contador.value=+ String(iRes);
   }