User Tools

Site Tools


appunti4s:introduzione_al_javascript
Ritorna al menù principale > indice

Javascript

Javascript è un linguaggio orientato ad oggetti e basato su prototipi. Ci sono tre possibili approcci:

  • affrontarlo come un semplice linguaggio procedurale
  • affrontarlo anche con oggetti/proprietà/metodi
  • affrontarlo anche come linguaggio funzionale/prototipale
  • affrontarlo come linguaggio asincrono

Vi sono alcuni aspetti del JavaScript che possono essere considerati progettati male e di cui se ne sconsiglia l'utilizzo, ad esempio:

  • classi ed ereditarietà classica

Debugger

Mozilla Firefox 19 (o superiore) ha un debugger:

Struttura sequenziale

  1. Esempio guidato (calcolatrice) javascript01.pdf
  2. Test di comprensione javascript02b.pdf
  3. Esercizio struttura sequenziale (età) javascript03.pdf
  4. Esercizio stipendio javascript08.pdf

Struttura condizionale

  1. Esempio guidato (condizione) javascript06.pdf
  2. Come combinare le condizioni condizioni.odp
  3. Esercizio (lavoro straordinario) javascript09.pdf
  4. Esercizio (votare) javascript10.pdf
  5. javascript12.odt (2 voti suff./insuff.)
  6. javascript14.pdf (piano cartesiano)
  7. javascript16.pdf (squenza crescente/decrescente)
  8. si puo' scrivere una condizione di aver riempito tutto il form?

Oggetti, metodi e proprietà

  1. Introduzione: oggetti_metodi.pdf
  2. Esempio 1 creare oggetti
  3. oggetti e metodi Esempio 2
  4. esempio con funzione che restituisce qualcosa:
    • somma(), window.open(), document.getElementById()
  5. esempio con proprietà:
    • forms[][].value, stringa.length, Date.now

Struttura iterativa

  1. Esempio guidato sulla struttura iterativa javascript20.pdf
  2. Esercizi svolti sui contatori javascript25.pdf
  3. creare un paragrafo, appenderlo a body, creare un nodotesto, appenderlo al paragr.
  4. creare 10 paragrafi, appenderli, creare 10 nodotesto, appenderli
  5. creare una casella di input dentro un form esistente (con pulsante)
  6. creare una casella di input dentro un paragrafo e modificare type in password
    • crearne 10 come sopra
  7. creare una casella di input dentro un paragrafo dentro un form
  8. creare una casella di input con testo davanti
  9. Scrivere un programma che chieda cinque numeri e ne visualizzi la somma
    1. in questo caso non si puo' utilizzare solo un totalizzatore, ma e' obbligatorio memorizzare prima tutti i dati in un array… quindi è necessario una iterazione per riempire l'array e un'altra per sommare i valori.
  10. visualizzare la tabellina del 4
  11. visualizzare tutte le tabelline da 1 fino a 10.
  12. Numero di ripetizioni sconosciuto (svolto in fondo a questa pagina)
  13. Gioco della battaglia navale

Approfondimento

  1. Tipi di dato javascript04.pdf
  2. usare var? Se uso var la variabile è sempre globale, anche dentro una funzione (w3schools)
  1. Esempio in fondo pagina?
  2. La funzione del Javascript (da revisionare perché contiene errori)

Esempio in javascript

calcolatrice-form.html
<html>
	<head>
		<script type="text/javascript">
		function funzioneProdotto()
		{
			var numero1=document.forms["formNumerico"]["numero1"].value;
			var numero2=document.forms["formNumerico"]["numero2"].value;
 
			if (numero1!=null && numero1!="" && numero2!=null && numero2!="")
			{
				risultato=numero1*numero2;
				window.alert("Risultato: "  + risultato );
			}
		}
		</script>
	</head>
 
	<body>
 
		<form id="formNumerico" action="" method="" >
			<input name="numero1" type="text" />
			<input name="numero2" type="text" />
			<input type="button" value="invia!" onclick="funzioneProdotto()" />
 
		</form>
	</body>
 
</html>
calcolatrice-prompt.html
<html>
	<head>
		<script type="text/javascript">
		function funzioneProdotto()
		{
			var numero1=prompt("Scrivi il primo numero","");
			var numero2=prompt("Scrivi il primo numero","");
			if (numero1!=null && numero1!="" && numero2!=null && numero2!="")
			{
				risultato=numero1*numero2;
				window.alert("Risultato: "  + risultato );
			}
		}
		</script>
	</head>
 
	<body">
 
	</body>
 
</html>
pagina.html
<html>
	<head>
		<script type="text/javascript">
		function laMiaFunzione()
		{
			var nome=prompt("Scrivi il tuo nome","Mario");
			if (nome!=null && nome!="")
			{
				document.write("<h1>Ciao " + nome + "! </h1>");
			}
		}
		</script>
	</head>
 
	<body onload="laMiaFunzione()">
         <h2> questa pagina simula una calcolatrice </h2>
 
	</body>
 
</html>

bottoni.html
<html>
  <head>
  <title>
  Esempio getElementById
  </title>
 
  <script type="text/javascript">
  function aggiungiColore() {
  document.getElementById('mio').style.backgroundColor='lime';
  }
  </script>
  </head>
  <body>
  <div style="background-color:pink; text-align:center;">
  <h1>getElementById</h1>
  </div>
 
  <div id="mio" style="background-color:yellow; height:100px; margin:20px;">
    il javascript può essere usato per modificare, in seguito ad eventi, 
    il codice HTML e il codice CSS
  </div>
 
  <input type="button" value="Colorami" 
   onClick="aggiungiColore();" />
 
  </body>
</html>

appendere.html
<html>
	<head> <title>modifica elemento esistente</title>
		<script>
		function appendi()
		{
			var paragrafo=document.createElement("P");
			var testo=document.createTextNode("ciao");
			var grassetto=document.createElement("STRONG");
                        document.body.appendChild(paragrafo);
			paragrafo.appendChild(grassetto);
			grassetto.appendChild(testo);
		}
		</script>
 
	</head>
	<body>
		<p>parola</p>
		<input type="button" onClick="appendi()">
	</body>
</html>
creare.html
<html>
	<head> <title>crea nuovo elemento </title>
		<script>
		function crea()
		{
			var testo=document.createTextNode("qui password");
			var paragrafo=document.createElement("P");
			var campo=document.createElement("INPUT");
			document.body.appendChild(paragrafo);
			paragrafo.appendChild(testo);
			paragrafo.appendChild(campo);
			campo.name="pass";
			campo.type="password";
			campo.onclick="crea()"; // non funziona :(
 
		}
		</script>
 
	</head>
	<body>
		<input type="button" onClick="crea()">
	</body>
</html>
elenco.html
<html>
	<head> <title>array di elementi </title>
		<script>
 
		function mostramolti()
		{
			var elenco= new Array(96,97,98,99);
			var dimensione = elenco.length;
			for (n=0; n<dimensione; n=n+1)
			{
				var elementoP=document.createElement("P");
				var testo="posiz. "+n+", valore "+elenco[n];
				var elementoT=document.createTextNode(testo);
				document.body.appendChild(elementoP);
				elementoP.appendChild(elementoT);
			}
		}
		</script>
 
	</head>
	<body>
		<input type="button" onClick="mostramolti()">
	</body>
</html>
totale5.html
<html>
	<head> <title>array di elementi </title>
		<script>
		function calcola()
		{	
			var voti= new Array();
			totale=0;
			for (n=0; n<5; n=n+1)
			{			
				voti[n]=document.forms["numeri"]["txtvoto"+n].value;
				totale=totale+parseInt(voti[n]);
			}
			document.write("<p>totale "+totale+"</p>");
 
		}		
 
		function aggiungi5Input()
		{   
			var formNumeri=document.getElementById("numeri");
 
			for (n=0; n<5; n=n+1)
			{
				testo="voto "+n;
				elementoP=document.createElement("P");
				elementoT=document.createTextNode(testo);
				elementoI=document.createElement("INPUT");
				formNumeri.appendChild(elementoP);
				elementoP.appendChild(elementoT);
				elementoP.appendChild(elementoI);
				elementoI.type="text";
				elementoI.name="txtvoto"+n;
			}
		}
 
		</script>
 
	</head>
	<body onload="aggiungi5Input()">
 
		<form id="numeri" >	
				<input type="button" value="sommare" onClick="calcola()">
		</form>
 
	</body>
</html>
voti.html
<html>
	<head> <title>array di elementi </title>
		<script>
		function aggiungiInput()
		{	var dimensione=document.forms["strumenti"]["txtquanti"].value;
 
			for (n=0; n<dimensione; n=n+1)
			{
				testo="voto "+n;
				var formNumeri=document.getElementById("numeri");
				var elementoP=document.createElement("P");
				var elementoT=document.createTextNode(testo);
				var elementoI=document.createElement("INPUT");
				formNumeri.appendChild(elementoP);
				elementoP.appendChild(elementoT);
				elementoP.appendChild(elementoI);
				elementoI.type="text";
				elementoI.name="txtvoto"+n;
			}
 
		}		
 
		function calcola()
 
		{   
			var voti= new Array();
 
			var dimensione=document.forms["strumenti"]["txtquanti"].value;
			totale=0;
			for (n=0; n<dimensione; n=n+1)
			{
				voti[n]=parseInt(document.forms["numeri"]["txtvoto"+n].value);
				totale=totale+parseInt(voti[n]);
			}
 
			for (n=0; n<dimensione; n=n+1)
			{
				document.write(voti[n]);
			}
			document.write("<p>totale "+totale+"</p>");
			document.write("<p>media "+(totale/n)+"</p>");
		}
		</script>
 
	</head>
	<body>
 
		<form id="strumenti" >	
		Inserire <input type="text" name="txtquanti"> elementi di input	
		<input type="button" value="aggiungi" onClick="aggiungiInput()">		
		</form>	
 
		<form id="numeri" >	
			<input type="button" value="calcola" onClick="calcola()">
		</form>
 
	</body>
</html>
appunti4s/introduzione_al_javascript.txt · Last modified: 2023/09/19 23:27 by profpro