<script src="modulo.js" type="text/javascript"></script>
scambio valori
var numero1 = prompt("dimmi il primo numero"); numero1 = numero1 * 1; var numero2 = prompt("dimmi il secondo numero"); numero2 = numero2 * 1; var scambio; if (numero1>0 && numero2>0){scambio = numero1; numero1 = numero2; numero2 = scambio; alert("Ho scambiato i valori:"+ numero1 + "" + numero2); } else alert("valori errati");
selezione binaria nidificata
var voto = prompt("inserisci voto da 1 a 10"); voto = voto * 1; var risultato; if (voto<6) { risultato = insufficiente; } else if (voto==6) { risultato = sufficiente;} else if (voto>6) { risultato = più che sufficiente; } document.write(risultato);
massimo valore
var x = 24; x = x * 1; var y = 15; y = y * 1; var z = 18; z = z * 1; massimoValore = x; if (y> massimoValore) { massimoValore = y; } else (z> massimoValore){ massimoValore = z; } document.write(massimoValore);
ciclo applicato per gli ARRAY
var urls = new Array(3); urls[0] = "www.unito.it"; urls[1] = "www.di.unito.it"; urls[2] = "www.dams.unito.it "; var unLink = ""; var lunghezza = urls.length; for(i=0;i<lunghezza;i++) { document.write ”<a href=‘”> + urls[i] + “’>”; document.write urls[i] + “</a>”; }
check box
function controlla(){ var check = true; if (document.frm.autorizzazione.checked==false) { alert("Deve dare l'autorizzazione al trattamento dei dati personali. Grazie"); check = false; } return check; }
invio
function controllaInvio(){
if ((document.getElementById("nome").value=="") || (document.getElementById("nome").value==null)) { alert("Il campo nome è obbligatorio"); document.getElementById("nome").focus(); return false; } if ((document.getElementById("cognome").value=="") || (document.getElementById("cognome").value==null)) { alert("Il campo cognome è obbligatorio") document.getElementById("cognome").focus(); return false; } if (document.getElementById("mail").value==""){ alert("La mail è obbligatoria"); document.getElementById("mail").focus(); return false; } if ((document.getElementById("mail").value.indexOf('@')==-1)|| (document.getElementById("mail").value.indexOf('.')==-1)){ alert("La mail non è corretta"); document.getElementById("mail").focus(); return false; }
altro form
<!DOCTYPE html>
<html> <body> <p>Enter a number and click OK:</p> <input id="id1" type="number" max="100"> <button onclick="myFunction()">OK</button> <p>If the number is greater than 100 (the input's max attribute), an error message will be displayed.</p> <p id="demo"></p> <script> function myFunction() { var txt = ""; if (document.getElementById("id1").validity.rangeOverflow) { txt = "Value too large"; } else { txt = "Input OK"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
ancora
<!DOCTYPE html>
<html> <body> <input id="id1" type="text" required> <input id="id2" type="text" required> <button onclick="myFunction()">OK</button> <p id="demo"></p> <script> function myFunction() { var txt = ""; if ((document.getElementById("id1").validity.valueMissing) && (document.getElementById("id2").validity.valueMissing)){ txt = "mancano i dati"; } else { txt = "Input OK"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
ciclo for 1
for (var i=20;i<=40; i++){ document.write(i + ” <br> "); }
ciclo for 2
var parziale=0; for (var i=1;i<=10; i++){ parziale= 3* i; document.write(parziale + "<br>"); }
set data
var d = new Date();d.setYear (2010); d.setMonth (3);d.setDate (23);document.write(d);
dado
var dado = Math.floor(Math.random( ) * 6) + 1; alert("risultato" + dado);