Sie sind auf Seite 1von 2

Ejemplos de Javascript

Escribir y alerta
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
Texto HTML
<script type="text/javascript">
alert("Mensaje en javascript")
</script>
</body>
</html>

Mostrar en una ventana el valor introducido en una caja de texto


<html>
<head>
<script type="text/javascript">
function myfunction(txt){
alert(txt)
}
</script>
</head>
<body>
<form name="form1">
<input name="entrada">
<input type="button"
onclick="myfunction(form1.entrada.value)"value="Call function">
</form>
<p>By pressing the button, a function with an argument will be
called.
The function will alert this argument.
</p>
</body>
</html>

Bucle para poner ttulos


<html>
<body>
<script type="text/javascript">
for (i = 1; i <= 6; i++){
document.write("<h" + i + ">Cabecera de nivel " + i)
document.write("</h" + i + ">")
}
</script>
</body>
</html>

Abrir una ventana


<html>
<head>
<script language=javascript>
function openwindow() {
m = window.open("http://www.tecnun.es");
}
function closewindow() {
m.close()
}
</script>
</head>
<body>
<form>
<input type=button value="Open Window" onclick="openwindow()">
<input type=button value="Close Window" onclick="closewindow()">
</form>
</body>
</html>

Validacion de un formulario
<html>
<head>
<script type="text/javascript">
function validate(){
x=document.myForm
txt=x.myInput.value
if (txt>=1 && txt<=5){
return true
} else {
if (txt < 1) alert("Es menor que 1, debe estar entre 1 y 5")
else if (txt > 1) alert("Es mayor que 5, debe estar entre 1
y 5")
else alert("No es vlido, debe estar entre 1 y 5")
return false
}
}
</script>
</head>
<body>
<form name="myForm" action="#" onSubmit="return validate()">
Enter a value from 1 to 5: <input type="text" name="myInput">
<input type="submit" value="Send input">
</form>
</body>
</html>

Das könnte Ihnen auch gefallen