SlideShare a Scribd company logo
1 of 22
Download to read offline
Javascript [7]
Matteo Magni
Spesso ci capita di dover riscrivere più volte
    lo stesso codice per fare la stessa
                operazione.
Developer
     Ma il programmatore è
     tendenzialmente un
     pigro.

     Sceglierò sempre un
     pigro per fare un lavoro
     difficile, perché troverà
     sempre un modo facile
     per farlo
     (Bill Gates)
Programmazione funzionale
         Per questo nasce la
         programmazione funzionale
         che ci permette di riutilizzare
         tutte le volte che ci serve una
         porzione di codice senza
         doverla riscrivere.
         Non è l'unico vantaggio, ma
         sicuramente il più immediato.
Function
                        function pippo() {
Una funzione è una
                          // javascript code
porzione di codice
                        } 
che viene eseguita
solo quando è           pippo();
richiamata attraverso
il suo nome.
Function
                        function sum(first, 
Una funzione può        second) {
avere dei parametri       result first + 
in ingresso e un        second;
valore di ritorno, al     return result;
fine di essere          } 
parametrizzabile e di
poter gestire i suoi    alert(sum(1+2));
risultati
Visibilità variabili
Infatti se usiamo "var" all’interno
                                                             Tale variabile è dichiarata
di una funzione, dichiariamo                                 (definita e agisce) solo all’interno
una variabile "solo" interna alla                            della variabile. Se invece
funzione (locale).                                           rimuovete "var a" da dentro la
                                                             funzione, andrete a modificare la
a = 100;                                                     variabile a globale
function cambiaA(){                                          a = 100;
var a;                                                       function cambiaAA(){
a=10;                                                        a=10;
                                                             }
}
                                                             alert(a);
alert(a);
                  http://blog.merlinox.com/la-visibilit-delle-variabili-javascript/
Come facciamo a interagire con l'utente?
Attraverso gli eventi, cioè qualcosa che
        accade nel documento.
Eventi
<TAG nomeEvento=”Javascript da eseguire”>


<h1 id="myHeader" onclick="getValue()">Click 
me!</h1>
Oggetto Event


   L'oggetto event permette di ottenere
informazioni sull'evento appena scaturito,
 come l'elemento che lo ha generato, o la
           posizione del mouse.
Event

Possiamo utilizzarlo dentro alla
dichiarazione dell'evento, ad esempio:
<a
onmouseover="alert('Il mouse è alla posizione ' + 
event.screenX + ', ' + event.screenY +  'dello schermo');"
>
link
</a>

       http://it.wikibooks.org/wiki/JavaScript/Gli_eventi_nel_DOM
Oggetto Event
Rispetto all'utilizzo di questo oggetto, bisogna fare attenzione
quando si richiama da delle funzioni handler: l'oggetto event
infatti ha una visibilità privata, non può quindi essere richiamato
esternamente alla dichiarazione dell'evento. Per ovviare a
questo problema è sufficiente passare l'oggetto event come
argomento. Ad esempio, si crea la funzione:
function posizione (e) {
alert('Il mouse è alla posizione ' + e.screenX + ', ' + e.screenY +  
'dello schermo');
}
Nell'HTML si inserirà:
<a href="pagina.html" onmouseover="posizione(event);">link</a>
Proprietà Event
  timestamp (funziona solo su FF) restituisce la data e l'ora in cui si è verificato l'evento;
•
  target e relatedTarget (srcElement e toElement su Internet Explorer) restituiscono
•
  rispettivamente il nodo che ha generato l'evento e il nodo su cui probabilmente sarà il
  mouse dopo che l'evento si sarà concluso (ad esempio è utile per gli eventi mouseOut).
  Quando si usano queste proprietà bisogna prima controllare il browser in uso
  dall'utente (vedi questa pagina);
  altKey, ctrlKey e shiftKey indicano rispettivamente se è premuto il tasto alt, ctrl o shift
•
  mentre avviene evento;
  button indica quale pulsante del mouse è stato premuto (0 il tasto sinistro, 1 quello
•
  destro);
  clientX e clientY indicano le posizioni del cursore rispetto alla finestra del browser (a
•
  partire dall'angolo in altro a destra);
  screenX e screenY indicano le posizioni del cursore rispetto allo schermo dell'utente
•
  (a partire dall'angolo in altro a destra).
Esempio
    
<script>
function EventFunc(e) {
  var target = e.target;
  target.style.color = 'red';
}
</script>
</head>
<body>
Event<br/>
<a onclick="EventFunc(event);" >link</a>
</body>
Tipi di eventi
Load
• onload → quando l'oggetto è
  completamente caricato [body, img]
<img src=”pippo.jpg” 
onload=”alert(‘pippo’);”>
Mouse
• onmouseover → quando il mouse passa
  sopra l'elemento
<a onmouseover=”alert(‘pippo’);”>
• onmouseout → quando il mouse esce
  fuori dall'area sensibile
<a onmouseout=”alert(‘pippo’);”>
Click
• onclick → quando l'utente clicca
  sull'elemento
<a onclick=”alert(‘pippo’);”>
Form - input
• onkeypress → quando in un input type viene
  premuto un tasto della tastiera
<textarea onkeypress=”alert(‘pippo’);”>
• onchange → quando l'elemento cambia
<select onchange=”alert(‘pippo’);”>
…
</select>
Form - input
• onsubmit → quando si invia la form
<form name=”pippo” onsubmit=”alert(‘pippo’);”>
• onfocus → quando l'elemento diventa attivo
<input type=”text” onfocus=”alert(‘pippo’);”>
• onblur → quando l'elemento non è più attivo
<input type=”text” onblur=”alert(‘pippo’);”>
Domande?

               Slide:
http://www.slideshare.net/ilbonzo
               Code:
https://github.com/ilbonzo/Cypher
                mail:
        matteo@magni.me

More Related Content

Viewers also liked

Faith in the midst of sickness
Faith in the midst of sickness Faith in the midst of sickness
Faith in the midst of sickness Alex Tang
 
Sigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship Packet
Sigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship PacketSigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship Packet
Sigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship PacketDillard University Library
 
Fairfield University 2010 Summer Conference June 2nd 4th
Fairfield University 2010 Summer Conference June 2nd 4thFairfield University 2010 Summer Conference June 2nd 4th
Fairfield University 2010 Summer Conference June 2nd 4thDillard University Library
 
If Products Tell Their Stories March 1 2010
If Products Tell Their Stories March 1 2010If Products Tell Their Stories March 1 2010
If Products Tell Their Stories March 1 2010Jennifer van der Meer
 
PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」
PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」
PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」Ryo YOSHIKAWA
 
Trade Union Freedom Fact Sheet Cnv Internationaal
Trade Union Freedom Fact Sheet Cnv InternationaalTrade Union Freedom Fact Sheet Cnv Internationaal
Trade Union Freedom Fact Sheet Cnv InternationaalCNV Vakcentrale
 
6 bible and postmodern context (2)
6 bible and postmodern context (2)6 bible and postmodern context (2)
6 bible and postmodern context (2)joshva raja john
 
Pulses - GREEK TEAM - IOANNINA
Pulses - GREEK TEAM - IOANNINAPulses - GREEK TEAM - IOANNINA
Pulses - GREEK TEAM - IOANNINAerapti
 

Viewers also liked (20)

10 Things Yacht Executives Need to know about Internet Marketing
10 Things Yacht Executives Need to know about Internet Marketing10 Things Yacht Executives Need to know about Internet Marketing
10 Things Yacht Executives Need to know about Internet Marketing
 
Faith in the midst of sickness
Faith in the midst of sickness Faith in the midst of sickness
Faith in the midst of sickness
 
Sigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship Packet
Sigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship PacketSigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship Packet
Sigma Gamma Rho Sorority Inc. Epsilon Phi Sigma 2012 Scholarship Packet
 
Fairfield University 2010 Summer Conference June 2nd 4th
Fairfield University 2010 Summer Conference June 2nd 4thFairfield University 2010 Summer Conference June 2nd 4th
Fairfield University 2010 Summer Conference June 2nd 4th
 
Using Direct Response Strategies to Dominate in Social Media
Using Direct Response Strategies to Dominate in Social MediaUsing Direct Response Strategies to Dominate in Social Media
Using Direct Response Strategies to Dominate in Social Media
 
If Products Tell Their Stories March 1 2010
If Products Tell Their Stories March 1 2010If Products Tell Their Stories March 1 2010
If Products Tell Their Stories March 1 2010
 
Character Costumes
Character CostumesCharacter Costumes
Character Costumes
 
Class Show
Class ShowClass Show
Class Show
 
26.10.2012, NEWSWIRE, Issue 245
26.10.2012, NEWSWIRE, Issue 24526.10.2012, NEWSWIRE, Issue 245
26.10.2012, NEWSWIRE, Issue 245
 
PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」
PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」
PCC2011「動画配信サービスによる市民参加型イベントの配信実験と考察」
 
El temps és or.
El temps és or.El temps és or.
El temps és or.
 
Anak punk
Anak punkAnak punk
Anak punk
 
Služba EOD v NTK
Služba EOD v NTKSlužba EOD v NTK
Služba EOD v NTK
 
Trade Union Freedom Fact Sheet Cnv Internationaal
Trade Union Freedom Fact Sheet Cnv InternationaalTrade Union Freedom Fact Sheet Cnv Internationaal
Trade Union Freedom Fact Sheet Cnv Internationaal
 
6 bible and postmodern context (2)
6 bible and postmodern context (2)6 bible and postmodern context (2)
6 bible and postmodern context (2)
 
LeadershipPlan
LeadershipPlanLeadershipPlan
LeadershipPlan
 
Pulses - GREEK TEAM - IOANNINA
Pulses - GREEK TEAM - IOANNINAPulses - GREEK TEAM - IOANNINA
Pulses - GREEK TEAM - IOANNINA
 
Conventions
ConventionsConventions
Conventions
 
Poem On Dark Light
Poem On Dark LightPoem On Dark Light
Poem On Dark Light
 
D0655022414
D0655022414D0655022414
D0655022414
 

Similar to Javascript - 7 | WebMaster & WebDesigner

Cattive abitudini e-lineeguida
Cattive abitudini e-lineeguidaCattive abitudini e-lineeguida
Cattive abitudini e-lineeguidaRobert Casanova
 
Intercettare gli eventi di mouse e tastiera
Intercettare gli eventi di mouse e tastieraIntercettare gli eventi di mouse e tastiera
Intercettare gli eventi di mouse e tastieraFederico Paparoni
 
Two months of Kotlin
Two months of KotlinTwo months of Kotlin
Two months of KotlinErik Minarini
 
Primo Incontro Con Scala
Primo Incontro Con ScalaPrimo Incontro Con Scala
Primo Incontro Con ScalaFranco Lombardo
 
Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)Davide Cerbo
 
Write less do more...with jQuery
Write less do more...with jQueryWrite less do more...with jQuery
Write less do more...with jQueryXeDotNet
 
Javascript Camp - Listener Per Eventi
Javascript Camp - Listener Per EventiJavascript Camp - Listener Per Eventi
Javascript Camp - Listener Per EventiSimone Gentili
 
C(99) gtk 01 - introduzione e finestre
C(99) gtk   01 - introduzione e finestreC(99) gtk   01 - introduzione e finestre
C(99) gtk 01 - introduzione e finestreMaurizio Carboni
 
Web advanced-01-asincrono
Web advanced-01-asincronoWeb advanced-01-asincrono
Web advanced-01-asincronoStudiabo
 
Programmazione mobile: ANDROID
Programmazione mobile: ANDROIDProgrammazione mobile: ANDROID
Programmazione mobile: ANDROIDPaolo Tosato
 
Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)Marcello Missiroli
 
How To React - Gestione Stato Applicativo
How To React - Gestione Stato ApplicativoHow To React - Gestione Stato Applicativo
How To React - Gestione Stato ApplicativoAldoNoschese
 

Similar to Javascript - 7 | WebMaster & WebDesigner (20)

Cattive abitudini e-lineeguida
Cattive abitudini e-lineeguidaCattive abitudini e-lineeguida
Cattive abitudini e-lineeguida
 
Js intro
Js introJs intro
Js intro
 
Intercettare gli eventi di mouse e tastiera
Intercettare gli eventi di mouse e tastieraIntercettare gli eventi di mouse e tastiera
Intercettare gli eventi di mouse e tastiera
 
Two months of Kotlin
Two months of KotlinTwo months of Kotlin
Two months of Kotlin
 
Primo Incontro Con Scala
Primo Incontro Con ScalaPrimo Incontro Con Scala
Primo Incontro Con Scala
 
7 Sottoprogrammi
7   Sottoprogrammi7   Sottoprogrammi
7 Sottoprogrammi
 
Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)
 
2007 Py05 gtk
2007 Py05 gtk2007 Py05 gtk
2007 Py05 gtk
 
introduzione a symfony 2
introduzione a symfony 2 introduzione a symfony 2
introduzione a symfony 2
 
Write less do more...with jQuery
Write less do more...with jQueryWrite less do more...with jQuery
Write less do more...with jQuery
 
XPages Tips & Tricks, #dd13
XPages Tips & Tricks, #dd13XPages Tips & Tricks, #dd13
XPages Tips & Tricks, #dd13
 
Javascript Camp - Listener Per Eventi
Javascript Camp - Listener Per EventiJavascript Camp - Listener Per Eventi
Javascript Camp - Listener Per Eventi
 
Pillole di C++
Pillole di C++Pillole di C++
Pillole di C++
 
#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2
 
C(99) gtk 01 - introduzione e finestre
C(99) gtk   01 - introduzione e finestreC(99) gtk   01 - introduzione e finestre
C(99) gtk 01 - introduzione e finestre
 
Web advanced-01-asincrono
Web advanced-01-asincronoWeb advanced-01-asincrono
Web advanced-01-asincrono
 
Programmazione mobile: ANDROID
Programmazione mobile: ANDROIDProgrammazione mobile: ANDROID
Programmazione mobile: ANDROID
 
iContract
iContractiContract
iContract
 
Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)
 
How To React - Gestione Stato Applicativo
How To React - Gestione Stato ApplicativoHow To React - Gestione Stato Applicativo
How To React - Gestione Stato Applicativo
 

More from Matteo Magni

Introduzione DevOps con Ansible
Introduzione DevOps con AnsibleIntroduzione DevOps con Ansible
Introduzione DevOps con AnsibleMatteo Magni
 
HTML5 e Css3 - 7 | WebMaster & WebDesigner
HTML5 e Css3 - 7 | WebMaster & WebDesignerHTML5 e Css3 - 7 | WebMaster & WebDesigner
HTML5 e Css3 - 7 | WebMaster & WebDesignerMatteo Magni
 
HTML5 e Css3 - 6 | WebMaster & WebDesigner
HTML5 e Css3 - 6 | WebMaster & WebDesignerHTML5 e Css3 - 6 | WebMaster & WebDesigner
HTML5 e Css3 - 6 | WebMaster & WebDesignerMatteo Magni
 
HTML5 e Css3 - 5 | WebMaster & WebDesigner
HTML5 e Css3 - 5 | WebMaster & WebDesignerHTML5 e Css3 - 5 | WebMaster & WebDesigner
HTML5 e Css3 - 5 | WebMaster & WebDesignerMatteo Magni
 
HTML5 e Css3 - 4 | WebMaster & WebDesigner
HTML5 e Css3 - 4 | WebMaster & WebDesignerHTML5 e Css3 - 4 | WebMaster & WebDesigner
HTML5 e Css3 - 4 | WebMaster & WebDesignerMatteo Magni
 
HTML5 e Css3 - 3 | WebMaster & WebDesigner
HTML5 e Css3 - 3 | WebMaster & WebDesignerHTML5 e Css3 - 3 | WebMaster & WebDesigner
HTML5 e Css3 - 3 | WebMaster & WebDesignerMatteo Magni
 
HTML5 e Css3 - 2 | WebMaster & WebDesigner
HTML5 e Css3 - 2 | WebMaster & WebDesignerHTML5 e Css3 - 2 | WebMaster & WebDesigner
HTML5 e Css3 - 2 | WebMaster & WebDesignerMatteo Magni
 
HTML5 e Css3 - 1 | WebMaster & WebDesigner
HTML5 e Css3 - 1 | WebMaster & WebDesigner HTML5 e Css3 - 1 | WebMaster & WebDesigner
HTML5 e Css3 - 1 | WebMaster & WebDesigner Matteo Magni
 
jQuery - 5 | WebMaster & WebDesigner
jQuery - 5 | WebMaster & WebDesignerjQuery - 5 | WebMaster & WebDesigner
jQuery - 5 | WebMaster & WebDesignerMatteo Magni
 
jQuery - 4 | WebMaster & WebDesigner
jQuery - 4 | WebMaster & WebDesignerjQuery - 4 | WebMaster & WebDesigner
jQuery - 4 | WebMaster & WebDesignerMatteo Magni
 
jQuery - 3 | WebMaster & WebDesigner
jQuery - 3 | WebMaster & WebDesignerjQuery - 3 | WebMaster & WebDesigner
jQuery - 3 | WebMaster & WebDesignerMatteo Magni
 
jQuery - 2 | WebMaster & WebDesigner
jQuery - 2 | WebMaster & WebDesignerjQuery - 2 | WebMaster & WebDesigner
jQuery - 2 | WebMaster & WebDesignerMatteo Magni
 
jQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesignerjQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesignerMatteo Magni
 
Javascript - 6 | WebMaster & WebDesigner
Javascript - 6 | WebMaster & WebDesignerJavascript - 6 | WebMaster & WebDesigner
Javascript - 6 | WebMaster & WebDesignerMatteo Magni
 
Javascript - 5 | WebMaster & WebDesigner
Javascript - 5 | WebMaster & WebDesignerJavascript - 5 | WebMaster & WebDesigner
Javascript - 5 | WebMaster & WebDesignerMatteo Magni
 
Javascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesignerJavascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesignerMatteo Magni
 
Javascript - 3 | WebMaster & WebDesigner
Javascript - 3 | WebMaster & WebDesignerJavascript - 3 | WebMaster & WebDesigner
Javascript - 3 | WebMaster & WebDesignerMatteo Magni
 
Javascript - 2 | WebMaster & WebDesigner
Javascript - 2 | WebMaster & WebDesignerJavascript - 2 | WebMaster & WebDesigner
Javascript - 2 | WebMaster & WebDesignerMatteo Magni
 
Javascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesignerJavascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesignerMatteo Magni
 
Web Usability - 3 | WebMaster & WebDesigner
 Web Usability - 3 | WebMaster & WebDesigner Web Usability - 3 | WebMaster & WebDesigner
Web Usability - 3 | WebMaster & WebDesignerMatteo Magni
 

More from Matteo Magni (20)

Introduzione DevOps con Ansible
Introduzione DevOps con AnsibleIntroduzione DevOps con Ansible
Introduzione DevOps con Ansible
 
HTML5 e Css3 - 7 | WebMaster & WebDesigner
HTML5 e Css3 - 7 | WebMaster & WebDesignerHTML5 e Css3 - 7 | WebMaster & WebDesigner
HTML5 e Css3 - 7 | WebMaster & WebDesigner
 
HTML5 e Css3 - 6 | WebMaster & WebDesigner
HTML5 e Css3 - 6 | WebMaster & WebDesignerHTML5 e Css3 - 6 | WebMaster & WebDesigner
HTML5 e Css3 - 6 | WebMaster & WebDesigner
 
HTML5 e Css3 - 5 | WebMaster & WebDesigner
HTML5 e Css3 - 5 | WebMaster & WebDesignerHTML5 e Css3 - 5 | WebMaster & WebDesigner
HTML5 e Css3 - 5 | WebMaster & WebDesigner
 
HTML5 e Css3 - 4 | WebMaster & WebDesigner
HTML5 e Css3 - 4 | WebMaster & WebDesignerHTML5 e Css3 - 4 | WebMaster & WebDesigner
HTML5 e Css3 - 4 | WebMaster & WebDesigner
 
HTML5 e Css3 - 3 | WebMaster & WebDesigner
HTML5 e Css3 - 3 | WebMaster & WebDesignerHTML5 e Css3 - 3 | WebMaster & WebDesigner
HTML5 e Css3 - 3 | WebMaster & WebDesigner
 
HTML5 e Css3 - 2 | WebMaster & WebDesigner
HTML5 e Css3 - 2 | WebMaster & WebDesignerHTML5 e Css3 - 2 | WebMaster & WebDesigner
HTML5 e Css3 - 2 | WebMaster & WebDesigner
 
HTML5 e Css3 - 1 | WebMaster & WebDesigner
HTML5 e Css3 - 1 | WebMaster & WebDesigner HTML5 e Css3 - 1 | WebMaster & WebDesigner
HTML5 e Css3 - 1 | WebMaster & WebDesigner
 
jQuery - 5 | WebMaster & WebDesigner
jQuery - 5 | WebMaster & WebDesignerjQuery - 5 | WebMaster & WebDesigner
jQuery - 5 | WebMaster & WebDesigner
 
jQuery - 4 | WebMaster & WebDesigner
jQuery - 4 | WebMaster & WebDesignerjQuery - 4 | WebMaster & WebDesigner
jQuery - 4 | WebMaster & WebDesigner
 
jQuery - 3 | WebMaster & WebDesigner
jQuery - 3 | WebMaster & WebDesignerjQuery - 3 | WebMaster & WebDesigner
jQuery - 3 | WebMaster & WebDesigner
 
jQuery - 2 | WebMaster & WebDesigner
jQuery - 2 | WebMaster & WebDesignerjQuery - 2 | WebMaster & WebDesigner
jQuery - 2 | WebMaster & WebDesigner
 
jQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesignerjQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesigner
 
Javascript - 6 | WebMaster & WebDesigner
Javascript - 6 | WebMaster & WebDesignerJavascript - 6 | WebMaster & WebDesigner
Javascript - 6 | WebMaster & WebDesigner
 
Javascript - 5 | WebMaster & WebDesigner
Javascript - 5 | WebMaster & WebDesignerJavascript - 5 | WebMaster & WebDesigner
Javascript - 5 | WebMaster & WebDesigner
 
Javascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesignerJavascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesigner
 
Javascript - 3 | WebMaster & WebDesigner
Javascript - 3 | WebMaster & WebDesignerJavascript - 3 | WebMaster & WebDesigner
Javascript - 3 | WebMaster & WebDesigner
 
Javascript - 2 | WebMaster & WebDesigner
Javascript - 2 | WebMaster & WebDesignerJavascript - 2 | WebMaster & WebDesigner
Javascript - 2 | WebMaster & WebDesigner
 
Javascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesignerJavascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesigner
 
Web Usability - 3 | WebMaster & WebDesigner
 Web Usability - 3 | WebMaster & WebDesigner Web Usability - 3 | WebMaster & WebDesigner
Web Usability - 3 | WebMaster & WebDesigner
 

Javascript - 7 | WebMaster & WebDesigner

  • 2. Spesso ci capita di dover riscrivere più volte lo stesso codice per fare la stessa operazione.
  • 3. Developer Ma il programmatore è tendenzialmente un pigro. Sceglierò sempre un pigro per fare un lavoro difficile, perché troverà sempre un modo facile per farlo (Bill Gates)
  • 4. Programmazione funzionale Per questo nasce la programmazione funzionale che ci permette di riutilizzare tutte le volte che ci serve una porzione di codice senza doverla riscrivere. Non è l'unico vantaggio, ma sicuramente il più immediato.
  • 5. Function function pippo() { Una funzione è una   // javascript code porzione di codice }  che viene eseguita solo quando è pippo(); richiamata attraverso il suo nome.
  • 6. Function function sum(first,  Una funzione può second) { avere dei parametri   result first +  in ingresso e un second; valore di ritorno, al   return result; fine di essere }  parametrizzabile e di poter gestire i suoi alert(sum(1+2)); risultati
  • 7. Visibilità variabili Infatti se usiamo "var" all’interno Tale variabile è dichiarata di una funzione, dichiariamo (definita e agisce) solo all’interno una variabile "solo" interna alla della variabile. Se invece funzione (locale). rimuovete "var a" da dentro la funzione, andrete a modificare la a = 100; variabile a globale function cambiaA(){ a = 100; var a; function cambiaAA(){ a=10; a=10; } } alert(a); alert(a); http://blog.merlinox.com/la-visibilit-delle-variabili-javascript/
  • 8. Come facciamo a interagire con l'utente?
  • 9. Attraverso gli eventi, cioè qualcosa che accade nel documento.
  • 11. Oggetto Event L'oggetto event permette di ottenere informazioni sull'evento appena scaturito, come l'elemento che lo ha generato, o la posizione del mouse.
  • 12. Event Possiamo utilizzarlo dentro alla dichiarazione dell'evento, ad esempio: <a onmouseover="alert('Il mouse è alla posizione ' +  event.screenX + ', ' + event.screenY +  'dello schermo');" > link </a> http://it.wikibooks.org/wiki/JavaScript/Gli_eventi_nel_DOM
  • 13. Oggetto Event Rispetto all'utilizzo di questo oggetto, bisogna fare attenzione quando si richiama da delle funzioni handler: l'oggetto event infatti ha una visibilità privata, non può quindi essere richiamato esternamente alla dichiarazione dell'evento. Per ovviare a questo problema è sufficiente passare l'oggetto event come argomento. Ad esempio, si crea la funzione: function posizione (e) { alert('Il mouse è alla posizione ' + e.screenX + ', ' + e.screenY +   'dello schermo'); } Nell'HTML si inserirà: <a href="pagina.html" onmouseover="posizione(event);">link</a>
  • 14. Proprietà Event timestamp (funziona solo su FF) restituisce la data e l'ora in cui si è verificato l'evento; • target e relatedTarget (srcElement e toElement su Internet Explorer) restituiscono • rispettivamente il nodo che ha generato l'evento e il nodo su cui probabilmente sarà il mouse dopo che l'evento si sarà concluso (ad esempio è utile per gli eventi mouseOut). Quando si usano queste proprietà bisogna prima controllare il browser in uso dall'utente (vedi questa pagina); altKey, ctrlKey e shiftKey indicano rispettivamente se è premuto il tasto alt, ctrl o shift • mentre avviene evento; button indica quale pulsante del mouse è stato premuto (0 il tasto sinistro, 1 quello • destro); clientX e clientY indicano le posizioni del cursore rispetto alla finestra del browser (a • partire dall'angolo in altro a destra); screenX e screenY indicano le posizioni del cursore rispetto allo schermo dell'utente • (a partire dall'angolo in altro a destra).
  • 17. Load • onload → quando l'oggetto è completamente caricato [body, img] <img src=”pippo.jpg”  onload=”alert(‘pippo’);”>
  • 18. Mouse • onmouseover → quando il mouse passa sopra l'elemento <a onmouseover=”alert(‘pippo’);”> • onmouseout → quando il mouse esce fuori dall'area sensibile <a onmouseout=”alert(‘pippo’);”>
  • 19. Click • onclick → quando l'utente clicca sull'elemento <a onclick=”alert(‘pippo’);”>
  • 20. Form - input • onkeypress → quando in un input type viene premuto un tasto della tastiera <textarea onkeypress=”alert(‘pippo’);”> • onchange → quando l'elemento cambia <select onchange=”alert(‘pippo’);”> … </select>
  • 21. Form - input • onsubmit → quando si invia la form <form name=”pippo” onsubmit=”alert(‘pippo’);”> • onfocus → quando l'elemento diventa attivo <input type=”text” onfocus=”alert(‘pippo’);”> • onblur → quando l'elemento non è più attivo <input type=”text” onblur=”alert(‘pippo’);”>
  • 22. Domande? Slide: http://www.slideshare.net/ilbonzo Code: https://github.com/ilbonzo/Cypher mail: matteo@magni.me