13. C ODICE JS <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>JavaScript!</title> <script type="text/javascript" src="my-script.js"></script> </head> <body> <!-- cdata per evitare problemi di validazione xhtml e commentati per impedire di non interpretare il codice javascript nei vecchi browser che non comprendono il tag cdata --> <script type="text/javascript"> // <![CDATA[ // ]]> </script> </body> </html>
43. Function operator : function name(argumentlist) block -->crea un oggetto di tipo Function. Senza nome crea funzione anonima
44. Function constructor : new Function(strings...) --> corpo della funzione come stringa come parametro (deprecata)
45.
46. Sintassi Javascript e Java a confronto JavaScript function Impiegato () { this.nome = ""; this.dipartimento = "generale"; } Java public class Impiegato { public String nome; public String dipartimento; public Impiegato () { this.nome = ""; this.dipartimento = "generale"; } } function Manager () { this.reports = []; } Manager.prototype = new Impiegato; function Progettista () { this.progetti = []; } Progettista.prototype = new Impiegato; public class Manager extends Impiegato { public String[] reports; public Manager () { this.reports = new String[0]; } } public class Progettista extends Impiegato { public String[] progetti; public Progettista () { this.progettista = new String[0]; } }
81. Ajax: operation if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","ajax_info.txt",true); //open(method,url,async) xmlhttp.send(); //xmlhttp.open("POST","ajax_test.asp",true); //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //settiamo l'handler onreadystatechange xmlhttp.onreadystatechange=function() //chiamata quando cambia il readyState { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } Property Description readyState Status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready status 200: "OK" 404: Page not found
204. J QUERY : UI $("#draggable").draggable(); $('div.myclass').droppable({ drop: function(ui,event){}, over: function...., //si attiva quando un draggable è dragged over out: function..., //si attiva mentre si fa il drag fuori dal droppable (se era dentro) tolerance: function //la condizione che lancia il drop viene richiamato il drop (Es. 'fit', 'touch',...) })