JavaScript - Problemas al agregar un archivo.js por etiqueta script

 
Vista:
sin imagen de perfil

Problemas al agregar un archivo.js por etiqueta script

Publicado por matias (6 intervenciones) el 25/12/2016 07:20:59
Hola, que tal? bueno, estoy aprendiendo AJAX y Javascript, y lo que quiero desarrolar es un Registro de usuraio, que cundo vas escribiendo tu nombre de usuario, por medio ajax comprueba si el nombre esta disponible. Eso ya lo logre, pero queria sacar al funcion de javascript en un .JS externo y llamarlo por una etiqueta scrpt para que el codigo sea mas prolijo, pero no importa lo que haga, nunca llama al a funcion en el JS. este es el codigo.

Si pudieran revisar el código o darme un buen tutorial de como se llama la funcion de un archivo js externo desde un html, lo agradeciere mucho

Este es el codigo html, donde si pongo la funcion del JS directamente aca, funciona perfecto,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<head>
	<title> Ttitulo</title>
 
<!-- ESTA ES LA ETIQEUTA DEL JS  QUE ESTA EL CODIGO AL FINAL DEL TEMA>
<script  type="text/javascript" src="./ver.js"></script>

</head>
<body>
<script  type="text/javascript" src="./ver.js"></script>
	<table>
		<tr>
			<td>
			<form action="Registrarce.php"  method="post">
				<td> Registrarce </tr>
				<tr> nombre : <input type="textarea" name="nombre" onkeyup="showHint(this.value)">
				<p>Suggestions: <span id="txtHint"></span></p>
				<tr> mail : <input type="textarea" >
				<tr> contraseña : <input type="textarea" >
				<input type="submit" name="Registrarce">
			</form>
			</td>

			<td>
				

			</td>
		</tr>	
	</table>

</body>
</html>


y Este es el codifo del vJS ver.js, que ya se que funciona, pero nunca es invocado por la funcion del HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function showHint(str) {
	 var xhttp;
	if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
 
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
 
        xmlhttp.open("GET", "IniciarSesionParaJS.php?q=" + str, true);
        xmlhttp.send();
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
 
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Problemas al agregar un archivo.js por etiqueta script

Publicado por matias (6 intervenciones) el 26/12/2016 00:34:12
PD :estyo utilizando xampp!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Alejandro
Val: 477
Bronce
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Problemas al agregar un archivo.js por etiqueta script

Publicado por Alejandro (130 intervenciones) el 26/12/2016 03:09:39
Hola matias, para incluir un script js tenes que hacerlo incluyendo las siguientes etiquetas
1
<script  type="text/javascript" src="ver.js"></script>
. Si tu archivo .js se encuentra al mismo nivel que el que contiene tu codigo html solo tenes que indicarle el nombre del archivo como en este caso. De lo contrario deberas darle la ruta relativa a este. Por temas de rendimiento es mejor incluir los scripts antes de la etiqueta de cierre de </body>.

Para agregar eventos, es mejor hacerlo mediante un manejador de eventos Ej:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function showHint() {
 
    var str = this.value;
 
    if (str.length === 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
 
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
 
        xmlhttp.open("GET", "IniciarSesionParaJS.php?q=" + str, true);
        xmlhttp.send();
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
 
    }
}
 
document.getElementsByName("nombre")[0].addEventListener('keyup', showHint);
Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar