Devil_Angel100
Power Member
JavaScript
Formulário
<script LANGUAGE="JavaScript">
function Validar(form)
{
if (form.Nome.value == "")
{ alert("Por favor insira o seu Nome"); form.Nome.focus(); return; }
if (form.Apelido.value == "")
{ alert("Por favor insira o seu Apelido"); form.Apelido.focus(); return; }
if (form.Email.value.indexOf('@', 0) == -1 ||
form.Email.value.indexOf('.', 0) == -1)
{ alert("Direcção de e-mail inválida"); form.Email.focus(); return; }
form.submit();
}
</script>
<body>
<form METHOD="post" ACTION="">
Nome:
<input type="text" name="Nome" size="20"><p>
Apelido:
<input type="text" name="Apelido" size="20"><p>
Telefone:
<input type="text" size="20"><p>
Email:
<input type="text" name="Email" size="20"><p>
<input
TYPE="button" VALUE="Enviar" onClick="Validar(this.form)">
<input type="reset" value="Apagar" size="20">
</body>
<head>
</head>
<body>
<script language="JavaScript">
images = new Array(3);
images[0] = "<a href = 'aleatorio.html'>FCPORTO</a>";
images[1] = "<a href = 'aleatorio.html'>SLBENFICA</a>";
images[2] = "<a href = 'aleatorio.html'>SPORTING</a>";
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
</script>
</body>
</html>
Apresenta nome pelo carácter inicial
<html><body>
<script type="text/javascript">
<!--
var nomes = new Array("Manuel", "Rita", "Joana", "Francisco", "Luís",
"Bernardo", "Helder", "Patrícia", "Hugo", "António", "Nuno");
for(var i=0; i < nomes.length;i++)
{
var nome = nomes
if(nome.charAt(0) == "H")
alert("O nome " + nome + " começa com a letra H");
}
// -->
</script>
<p> charAt(indice) - devolve o caracter que ocupa a posição indice da string</p>
</body></html>
Ampliar Imagens
<html>
<head>
<script type="text/javascript">
function imagem()
{
checkbox = document.getElementById("caixa_seleccao")
if(checkbox.checked==true)
{
document.getElementById("img_ff").src="benfica.jpg"
document.getElementById("img_ff").height=200
document.getElementById("img_ff").width=200
}
else
{
document.getElementById("img_ff").src="benfica.jpg"
document.getElementById("img_ff").height=50
document.getElementById("img_ff").width=50
}
}
</script>
</head>
<body>
<img id="img_ff" alt="Firefox" src="benfica.jpg" /><p>
<br />
<input id="caixa_seleccao" type="checkbox" onClick="imagem()" /> Ampliar
</body>
</html>
Activar Grupo de opções em função da opção escolhida no select
<script type="text/javascript">
function formAction()
{
var x=document.forms.myForm.mysele
alert(x.options[x.selectedIndex].text)
var y=x.options[x.selectedIndex].text
document.getElementById("ME").value=y
switch (x.options[x.selectedIndex].text)
{
case'Aplicações informáticas':
document.getElementById("1ME").checked=true
document.getElementById("2ME").checked=false
document.getElementById("3ME").checked=false
break;
case'Tecnologias':
document.getElementById("1ME").checked=false
document.getElementById("2ME").checked=true
document.getElementById("3ME").checked=false
break;
case'B. de Programação':
document.getElementById("1ME").checked=false
document.getElementById("2ME").checked=false
document.getElementById("3ME").checked=true
break;
}
}
</script><title></title></head><body>
<form name="myForm">
<select name="mysele">
<option selected="selected">
Aplicações informáticas
</option>
<option>
Tecnologias
</option>
<option>
B. de Programação
</option>
</select> <input type="button" onclick="formAction()" value="Mostrar o Texto da Opção Escolhida">
<input id="ME" type="text">
<br>
Opção 1: <input id="1ME" type="radio">
<br>
Opção 2: <input id="2ME" type="radio">
<br>
Opção 3: <input id="3ME" type="radio">
</form>
</body></html>
Cf2switch
<html>
<head>
<script type="text/javascript">
function minhafunc()
{
var y=0
var verdade=false
while (verdade==false)
{
var nome=prompt("Introduza o nº","")
x=nome
if (y>20)
{
breaK
}
else
{
switch (x)
{
case '12': case '23': case '64':
y++
document.write("acertou no número: "+ x)
document.write("Com "+ y +" tentativas")
verdade=true
break;
default:
y++
document.write("não acertou no número:" + x)
}
}
}
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="minhafunc()"
value="Chama a função">
</form>
<p>Pressionando o botão a função é chamada</p>
</body>
</html>
Obter o codigo do Caracter
<html><head>
<script type="text/javascript">
<!--
function codigo()
{
var str=document.getElementById("Intro").value
if (str!="")
{
var unicode=str.charCodeAt(0)
document.getElementById("unicode").value=unicode
}
}
// -->
</script><title></title></head><body>
<form action="javascript:;">
Escreva um carácter no campo seguinte:<br>
<input maxlength="1" onkeyup="codigo()" size="2" id="Intro" type="text">
<hr>
O código Unicode do carácter é:
<input size="3" id="unicode" type="text">
</form>
</body></html>
Mudar_o_valor_da_lista
<html><head>
<script type="text/javascript">
function mudaTexto()
{
var x=document.getElementById("cx")
x.options[x.selectedIndex].text="já revisto"
}
</script></head><body>
<form>
Selecciona o tema:
<select id="cx">
<option>Ancorensis</option>
<option>Ministério da Educação</option>
<option>Exames</option>
<option>Avaliação</option>
</select>
<br><br>
<input onclick="mudaTexto()" value="Muda o texto do valor seleccionado" type="button">
</form>
</body></html>
Converter texto em Maiusculas
<html><head>
<script type="text/javascript">
function convertmaius()
{
document.getElementById("pnome").value=document.getElementById("pnome").value.toUpperCase()
document.getElementById("unome").value=document.getElementById("unome").value.toUpperCase()
}
</script></head><body>
<form name="form1">
Primeiro nome: <input id="pnome" size="20" type="text">
<br><br>
Ultimo nome: <input id="unome" size="20" type="text">
<br><br>
Converter para Maiusculas
<input onclick="if (this.checked) {convertmaius()}" type="checkbox">
</form>
</body></html>
Imagem aleatória
<script language="javascript">
<!--
function CriaTab(n)
{
this.length = n
for (var x=0;x<=n;x++)
{ this[x] = "" }
}
var maxtam = 5;
var index = maxtam + 1;
ImgName = new CriaTab(maxtam)
ImgName[0] = "15156.jpg"
ImgName[1] = "cavalo.jpg"
ImgName[2] = "1156.jpg"
ImgName[3] = "peixe.jpg"
ImgName[4] = "5156.jpg"
while( index > maxtam ){
index = Math.round(Math.random()*100);
if ( index < maxtam )
document.write("<img src=" + ImgName[index] + ">");
}
//-->
</script>
Lista Url
<html><head>
<script type="text/javascript">
function link()
{
window.location=document.getElementById("menu").value
}
</script></head><body>
<form>
<select id="menu" onchange="link()">
<option>--Selecciona a pagina--</option>
<option value="http://www.ancorensis.pt">Ancorensis Coop. Ensino</option>
<option value="http://www.Microsoft.com">Microsoft</option>
<option value="http://www.sapo.pt">Sapo</option>
</select>
</form>
</body></html>
Formulário
<script LANGUAGE="JavaScript">
function Validar(form)
{
if (form.Nome.value == "")
{ alert("Por favor insira o seu Nome"); form.Nome.focus(); return; }
if (form.Apelido.value == "")
{ alert("Por favor insira o seu Apelido"); form.Apelido.focus(); return; }
if (form.Email.value.indexOf('@', 0) == -1 ||
form.Email.value.indexOf('.', 0) == -1)
{ alert("Direcção de e-mail inválida"); form.Email.focus(); return; }
form.submit();
}
</script>
<body>
<form METHOD="post" ACTION="">
Nome:
<input type="text" name="Nome" size="20"><p>
Apelido:
<input type="text" name="Apelido" size="20"><p>
Telefone:
<input type="text" size="20"><p>
Email:
<input type="text" name="Email" size="20"><p>
<input
TYPE="button" VALUE="Enviar" onClick="Validar(this.form)">
<input type="reset" value="Apagar" size="20">
</body>
Aleatório
<html>
<head>
</head>
<body>
<script language="JavaScript">
images = new Array(3);
images[0] = "<a href = 'aleatorio.html'>FCPORTO</a>";
images[1] = "<a href = 'aleatorio.html'>SLBENFICA</a>";
images[2] = "<a href = 'aleatorio.html'>SPORTING</a>";
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
</script>
</body>
</html>
Apresenta nome pelo carácter inicial
<script type="text/javascript">
<!--
var nomes = new Array("Manuel", "Rita", "Joana", "Francisco", "Luís",
"Bernardo", "Helder", "Patrícia", "Hugo", "António", "Nuno");
for(var i=0; i < nomes.length;i++)
{
var nome = nomes
if(nome.charAt(0) == "H")
alert("O nome " + nome + " começa com a letra H");
}
// -->
</script>
<p> charAt(indice) - devolve o caracter que ocupa a posição indice da string</p>
</body></html>
Ampliar Imagens
<html>
<head>
<script type="text/javascript">
function imagem()
{
checkbox = document.getElementById("caixa_seleccao")
if(checkbox.checked==true)
{
document.getElementById("img_ff").src="benfica.jpg"
document.getElementById("img_ff").height=200
document.getElementById("img_ff").width=200
}
else
{
document.getElementById("img_ff").src="benfica.jpg"
document.getElementById("img_ff").height=50
document.getElementById("img_ff").width=50
}
}
</script>
</head>
<body>
<img id="img_ff" alt="Firefox" src="benfica.jpg" /><p>
<br />
<input id="caixa_seleccao" type="checkbox" onClick="imagem()" /> Ampliar
</body>
</html>
Activar Grupo de opções em função da opção escolhida no select
<script type="text/javascript">
function formAction()
{
var x=document.forms.myForm.mysele
alert(x.options[x.selectedIndex].text)
var y=x.options[x.selectedIndex].text
document.getElementById("ME").value=y
switch (x.options[x.selectedIndex].text)
{
case'Aplicações informáticas':
document.getElementById("1ME").checked=true
document.getElementById("2ME").checked=false
document.getElementById("3ME").checked=false
break;
case'Tecnologias':
document.getElementById("1ME").checked=false
document.getElementById("2ME").checked=true
document.getElementById("3ME").checked=false
break;
case'B. de Programação':
document.getElementById("1ME").checked=false
document.getElementById("2ME").checked=false
document.getElementById("3ME").checked=true
break;
}
}
</script><title></title></head><body>
<form name="myForm">
<select name="mysele">
<option selected="selected">
Aplicações informáticas
</option>
<option>
Tecnologias
</option>
<option>
B. de Programação
</option>
</select> <input type="button" onclick="formAction()" value="Mostrar o Texto da Opção Escolhida">
<input id="ME" type="text">
<br>
Opção 1: <input id="1ME" type="radio">
<br>
Opção 2: <input id="2ME" type="radio">
<br>
Opção 3: <input id="3ME" type="radio">
</form>
</body></html>
Cf2switch
<html>
<head>
<script type="text/javascript">
function minhafunc()
{
var y=0
var verdade=false
while (verdade==false)
{
var nome=prompt("Introduza o nº","")
x=nome
if (y>20)
{
breaK
}
else
{
switch (x)
{
case '12': case '23': case '64':
y++
document.write("acertou no número: "+ x)
document.write("Com "+ y +" tentativas")
verdade=true
break;
default:
y++
document.write("não acertou no número:" + x)
}
}
}
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="minhafunc()"
value="Chama a função">
</form>
<p>Pressionando o botão a função é chamada</p>
</body>
</html>
Obter o codigo do Caracter
<html><head>
<script type="text/javascript">
<!--
function codigo()
{
var str=document.getElementById("Intro").value
if (str!="")
{
var unicode=str.charCodeAt(0)
document.getElementById("unicode").value=unicode
}
}
// -->
</script><title></title></head><body>
<form action="javascript:;">
Escreva um carácter no campo seguinte:<br>
<input maxlength="1" onkeyup="codigo()" size="2" id="Intro" type="text">
<hr>
O código Unicode do carácter é:
<input size="3" id="unicode" type="text">
</form>
</body></html>
Mudar_o_valor_da_lista
<html><head>
<script type="text/javascript">
function mudaTexto()
{
var x=document.getElementById("cx")
x.options[x.selectedIndex].text="já revisto"
}
</script></head><body>
<form>
Selecciona o tema:
<select id="cx">
<option>Ancorensis</option>
<option>Ministério da Educação</option>
<option>Exames</option>
<option>Avaliação</option>
</select>
<br><br>
<input onclick="mudaTexto()" value="Muda o texto do valor seleccionado" type="button">
</form>
</body></html>
Converter texto em Maiusculas
<html><head>
<script type="text/javascript">
function convertmaius()
{
document.getElementById("pnome").value=document.getElementById("pnome").value.toUpperCase()
document.getElementById("unome").value=document.getElementById("unome").value.toUpperCase()
}
</script></head><body>
<form name="form1">
Primeiro nome: <input id="pnome" size="20" type="text">
<br><br>
Ultimo nome: <input id="unome" size="20" type="text">
<br><br>
Converter para Maiusculas
<input onclick="if (this.checked) {convertmaius()}" type="checkbox">
</form>
</body></html>
Imagem aleatória
<script language="javascript">
<!--
function CriaTab(n)
{
this.length = n
for (var x=0;x<=n;x++)
{ this[x] = "" }
}
var maxtam = 5;
var index = maxtam + 1;
ImgName = new CriaTab(maxtam)
ImgName[0] = "15156.jpg"
ImgName[1] = "cavalo.jpg"
ImgName[2] = "1156.jpg"
ImgName[3] = "peixe.jpg"
ImgName[4] = "5156.jpg"
while( index > maxtam ){
index = Math.round(Math.random()*100);
if ( index < maxtam )
document.write("<img src=" + ImgName[index] + ">");
}
//-->
</script>
Lista Url
<html><head>
<script type="text/javascript">
function link()
{
window.location=document.getElementById("menu").value
}
</script></head><body>
<form>
<select id="menu" onchange="link()">
<option>--Selecciona a pagina--</option>
<option value="http://www.ancorensis.pt">Ancorensis Coop. Ensino</option>
<option value="http://www.Microsoft.com">Microsoft</option>
<option value="http://www.sapo.pt">Sapo</option>
</select>
</form>
</body></html>
Última edição: