----------------------------------------------------------------------------- ----------------------- HTML ESSENZIALE ---------------------------------- -----------------------------------------------------------------------------
HTML (minimo indispensabile): ... </table> (completa): <HEAD><SCRIPT>..(funzioni)..</SCRIPT></HEAD> <SCRIPT>..(richiamo funzioni)</SCRIPT><B> bold <I> italics <U> underlined <S> strikeout <H1>...<H6> header <SUB><SUP> sub/sup-script <FONT COLOR=red> colori <BR> break line <HR> horizzontal row <IMG SRC="immagine.gif"> immagine <DIV ALIGN="left"> .... </div> division aligned to left (right / middle) <TABLE BORDER><TR><TH> Tit.1 <TH> Tit.2 <TR><TD> a11 <TD> abcdefghijklmnopq <TR><TD>a21<TD>a22</TABLE> <UL> unordered list <LI> list item </UL> ( <OL> ordered list ) <A href="http://....."> testo </A> hyperlink (Anchor to hyper reference) <A href="mailto://....."> testo </A> link a indirizzo di posta <SPAN style="position:absolute; left:350; top:200;"> . </SPAN> scrive un punto in coordinate (350,200) (alla base della grafica) <XMP> eXaMPle : visualizza esattamente cosa segue fino a chiusura del tag <CODE> testo isospaziato <FORM NAME="dat"> modulo I/O <INPUT TYPE=TEXT NAME="c1" ONCHANGE="f1(dat.c1.value)"> casella I/O <INPUT TYPE=BUTTON VALUE="SUL BOTTONE" ONCLICK="f2(dat.c1.value)"> bottone <TEXTAREA NAME="at1" ROWS=10 COLS=40 ONCHANGE="f3(dat.at1.value)"> area I/O </FORM> <SCRIPT>..</SCRIPT> script JavaScript <SCRIPT SRC="indirizzo%20file"></SCRIPT> include un file di libreria JavaScript <APPLET CODE="nome.class" ></APPLET> inserisce una applet ----------------------------------------------------------------------------- ------------------------- JAVASCRIPT ---------------------------- ----------------------------------------------------------------------------- (minimo indispensabile): <SCRIPT> .. </SCRIPT> (completa): <HEAD><SCRIPT>..(funzioni)..</SCRIPT></HEAD> <SCRIPT>..(richiamo funzioni)</SCRIPT></textarea></code></ol></font></sup></sub></h6></h1></s></u></i></b> // attenzione a MAIUSCOLE e minuscole: sono diverse !!!!!!!!!!!!!!!!!!!!!!!!!!! // commento su una riga oppure:/* commento esteso su piu' righe */ // ------------------- VARIABILI ----------------------------------------------
Variabili var i=23,I='pippo',titolo="L'essere e il nulla",r=1.5E-234, vett=[1,4,3]; var mat=[[11,12],[21,22]],patt=/\d*/; var nDecimale=233,nOttale=0176, nHex=0xFFCA; var pioveOnonPiove=true; var pioveEnonPiove=false; var f=function(x){return x*x+4}; var punto =new Object(); punto.x=2.3;punto.y=-4.1; var punto={x:2.3,y:-4.1}; var rettangolo{SpgAltoSx:{x:punto.x,y:punto.y},SpgBassoDx:{x:punto.x+l,y:punto.y+h}} var w2=window.open("","finestra2");w2.document.writeln('ciao'); var a= new Array();a[0]=23;a[9999999999999999999999999999999999999998]=12; var epifania=new Date(98,0,6); epifania.setYear(epifania.getYear()+1); var infi=Number.POSITIVE_INFINITY,epsilon=Number.MIN_VALUE;
// ------------------- OPERATORI ---------------------------------------------- a= 8*8+8/8-Math.pow(2,3); a = a+1; a+=1; a++; a--; b *=2; c /=10 ; modulo10 = n % 10; if (a!=b){alert('diversi')}; // operatore "not-uguale" if !( (x>=2 && x<5) || (x>7 ){...} // Se non è vero che x appartiene a [2,5) oppure x>7 allora fai ... a=1;a=a<<7; // shift binario 7 posizioni (=2^7=128) a= a & 0xFF; // AND bit a bit con 11111111 a= b && c ; // a vero se b AND c ( b e c veri se diversi da zero); // ------------------- ISTRUZIONI ------------------------------------------- // INPUT..................................... var l=prompt("lato");var eu=prompt("valore Euro","1936.27"); // da finestrella var x=document.dat.at1.value; // Da una form chiamata dat con un input at1 // OUTPUT ................................... alert("area="+l*l); // su finestrella document.writeln("area=<font color=red>"+l*l+'</font>'); // su documento var w2=window.open("","finestra2");w2.document.writeln("area="+l*l);// su docum. 2 document.dat.at1.value = "area="+l*l // su una form // STRUTTURE ................................
if if(cond){istruzioni}; // if if(cond){}else{}; // if/else if(cond){} // if/else if else if(){} else if(){} else{}; x = (x>0)? x : -x ; // assegnazione condizionale
switch switch(a){ // if multiplo non necessariamente mutuamente esclusivo case 2:{istruzioni;break;} // se a==2 poi esclude altri case 3:{istruzioni;break;} // se a==3 poi esclude altri case a>(b*2):{istruzioni;break;} // se a>(b*2) (valutato in esecuzione) case -3:{istruzioni;} // se a==-3 analizzera' comunque a<100 (no break) case a<100:{istruzioni;break;} // ultimo caso previsto default:{istruzioni;break;} // in ogni altro caso (facoltativo) }
while
do while
while(cond){istruzioni}; // esegue ripetutamente se cond. (forse mai) es. var i=0;while(++i<=10){alert(i)}; // conta da 1 a 10 do{istruzioni}while(cond); // esegue ripetutamente se cond. (almeno una volta) es. do{a=prompt('vuoi smettere?')}while(a.toUpperCase()!='SI');
for 1) for(inizializzazione;cond PERMANENZA NEL CICLO;incremento){}; es. for(var i=10;i>=1;i--){alert(i)} // conta da 10 a 1 2) es. con rottura della ricerca (verifica se numero primo) cicloEsterno: for(var i=2,n=prompt('dimmi numero forse primo'),trovato=false; i<=n-1; i++){ cicloInterno: for(var j=2;j<=n-1;j++){ if(i*j==n){alert(i+'*'+j+'='+n+':non primo!');trovato=true;break cicloEsterno}; document.writeln('<br>provato '+i+'*'+j+'='+i*j); if(i*j>n){break cicloInterno}; } } if (!trovato) alert('Numero primo'); 3) es. con salto di una iterazione for (var x=-0.2;x<=0.2;x+=0.100) {if(x==0){continue} document.write(1/x+'<br>'); }
// ------------------- FUNZIONI ------------------------------------------- <head><script> // alcuni esempi function dw(x){document.writeln(x);} function quadrato(x){var y=x*x;return y;} function prodottoScalare(v1,v2){for(var i=0,s=0;i<=v1.length-1;i++){s+=v1[i]*v2[i]}; return s;} function fact(x){return ( (x<=1)? 1 : x*fact(x-1) ); }; </script></head> <script> dw('quadrato di 2='+quadrato(2)+'<br>'); dw('prodotto scalare di [1,4] e [2,-3]='+prodottoScalare([1,4],[2,-3])+'<br>'); dw('5!='+fact(5)); </script> function max(){ // funzione a numero di argomenti variabile var m=Number.NEGATIVE_INFINITY; for(var i=0;i<arguments.length;i++){ if (arguments[i]>m) {m= arguments[i];} } return m; } a=5;b=34;c=1;d=-12;e=21; alert(max(a,b)); alert(max(a,c,e)); alert(max(a,b,c,d,e)); // varie ricerche max alert(max.toString()); // visualizza il listato della funzione max funzione=prompt('fornire la funzione in x'); // chiede la funzione eval('function f(x){' + funzione + '}'); // crea la funzione dinamicamente alert (f.toString()); // segnala il suo valore per x=1 // ------------------- OGGETTI ------------------------------------------- function Complex(x,y){ this.r=x; this.i=y; this.modulo=function(){ return Math.sqrt(this.r*this.r+this.i*this.i); } this.coniuga=function(){ this.i *= -1; // effettua la coniugazione return (new Complex(this.r,this.i)); // ma la restituisce anche } this.toString=function(){ return ( this.r+ '+ ' +this.i+'<b> i</b> ' ); } }; Complex.somma = function(a,b){return new Complex( a.r+b.r, a.i+b.i );} Complex.zero = new Complex(0,0); a= new Complex(1,2); b= new Complex(-1,2); c = Complex.somma(a,b); document.writeln(a+'<br> '+b+'<br> '+c+'<br> '+c.coniuga()); // stampa: 1+ 2 i -1+ 2 i 0+ 4 i 0+ -4 i // Recupero delle proprietà e metodi di istanza: for (p in a){document.writeln('<br>'+p+'='+a[p])}; /* stampa: r=1 i=2 modulo=function(){ return Math.sqrt(this.r*this.r+this.i*this.i); } coniuga=function(){ this.i *= -1; // effettua la coniugazione return (new Complex(this.r,this.i)); // ma la restituisce anche } */ // Recupero delle proprietà e metodi di classe: for (p in Complex){document.writeln('<br>'+p+'='+Complex[p])}; /* stampa: somma=function(a,b){return new Complex( a.r+b.r, a.i+b.i );} zero=0+ 0 i */ // ------------------- ARRAY ------------------------------------------- // join e array di oggetti v = [new Complex(1,3),new Complex(-1,3),new Complex(1,-3)] document.writeln(v.join('<br>')); // stampa: 1+ 3 i ,-1+ 3 i ,1+ -3 i // split e regular espression s = " 12,23 4 5 65,2;66;1"; // una stringa contenente numeri v = s.split(/[,\s;]/) // crea un vettore dividendola dove trova "," spazio oppure ";" document.writeln(v.join()+'<br>'); // lo converte in stringa e la stampa: 12,23 4 5 65,2;66;1 document.writeln('<table border=1><tr><td>'+v.join('<tr><td>')+'</table>'); // lo converte in stringa separando gli elementi con '<tr><td>' // stampa quindi una colonna/tabella v.reverse(); // lo inverte document.writeln(v.join()+'<br>'); // v=[1,66,2,65,5,4,23,12] v=v.concat(222,333,444); // lo riscrive concatenandovi 3 elementi document.writeln(v.join()+'<br>'); // v=[1,66,2,65,5,4,23,12,222,333,444] v.sort() // lo ordina lessicograficamente document.writeln(v.join()+'<br>'); // v=[1,12,2,222,23,333,4,444,5,65,66] v.sort(function(a,b){return (a-b)});// lo ordina numericamente document.writeln(v.join()+'<br>'); // v=[1,2,4,5,12,23,65,66,222,333,444] v.sort(function(a,b){return (a%2-b%2)});// lo ordina sulla base del modulo 2 document.writeln(v.join()+'<br>'); // v=[2,4,12,66,222,444,1,5,23,65,333] v2=v.slice(0,3); // ne prende i primi tre elementi document.writeln(v2.join()+'<br>'); // v=[2,4,12] v3=v.slice(3,-1); // ne prende gli elementi dal quarto al penultimo document.writeln(v3.join()+'<br>'); // v=[66,222,444,1,5,23,65] stringa=v3.join(); // ------------------- ESPRESSIONI REGOLARI ---------------------------------- // sono le stesse del PERL var a="stringa molto (pippo) lunga con parola pippo sbagliata molte (pippo) volte"; a=a.replace(/pippo/gi,"pluto"); // Ora è a posto document.writeln(a.match(/[aml]/g));// segnala a,m,l,l,l,a,a,l,a,l,a,l,a,a,m,l,l,l // ------------------- FUNZIONI PROPRIETARIE --------------------------- var a=127.25;document.writeln(a.toString(2));//1111111.01 document.writeln(a.toString(3));//11201.02020202020202020202020202020 document.writeln((255323223).toString(16).toUpperCase());//F37EC57 document.writeln(parseInt("200",3));// 18 eval(stringa);//il valore di (stringa) diventa l'istruzione eseguita Math. abs .floor .round .ceil .exp .log .min(a,b) .max(a,b) .pow(base,espon.) .sqrt .sin .cos .tan .tan .asin .acos .PI .random() String.fromCharCode(i) // carattere unicode i stringa. anchor big bold charAt charCodeAt concat fixed fontcolor fontsize indexOf italics lastIndexOf link match replace search slice small split strike sub substring sup toLowerCase toUpperCase toSource // ------ LIBRERIA GRAFICA: http://digilander.iol.it/areatecno/funzioni.txt --- (da verificare: non consolidata) setColor("red"); point(x,y);punto(x,y); drawLine (x0,y0,x1,y1); drawRectangle(x0,y0,l,h) drawOval(x0,y0,r1,r2); drawPicture(x0,y0,nomeFile); moveAt(x,y); fillRectangle(x0,y0,l,h); fillOval(x0,y0,r1,r2); drawString(s,x0,x1);