SlideShare una empresa de Scribd logo
1 de 20
Descargar para leer sin conexión
JavaScript and AJAX
     by Frane Bandov
JavaScript ≠ Java
 ●   Developed in 1995/96 by Netscape
 ●   No technological relation to Java
      ●    Originally named LiveScript, but renamed to JavaScript
           for marketing purposes (Java hype in the mid-90s)
 ●   Standardized as ECMAScript (ECMA-262) in 1998
 ●   Very small object and instruction set → efficient
 ●   Runs in a sandbox
 ●   Manipulation of HTML documents via DOM

11/06/09              Frane Bandov: JavaScript and AJAX for Java Developers   2
JavaScript History                                source: Wikipedia

Version      Release    Description                    Netscape Firefox      Internet     Opera   Safari   Chrome
             date                                                            Exporer
1.0          03.1996                                   2.0                   3.0
1.1          08.1996                                   3.0
1.2          06.1997                                   4.0-4.05
1.3          10.1998    ECMA-262 1st edition /         4.06-
                        ECMA-262 2nd edition           4.7x
1.4                                                     Server
1.5          11.2000    ECMA-262 3rd edition           6.0         1.0       5, 6, 7, 8   6-9
1.6          11.2005    1.5 + Array extras + Array                 1.5                            3.0,
                        & String generics + E4X                                                   3.1
1.7          10.2006    1.6 + Pythonic generators                  2.0                            2.2,     1.0
                        + Iterators + let                                                         4.0
1.8          06.2008    1.7 + Generator                            3.0
                        expressions + Expression
                        closures
1.8.1                   1.8 + geringfügige                         3.5
                        Updates
1.9                     1.8.1 + ECMAScript 5                      4.0
  11/06/09              Compliance Bandov: JavaScript and AJAX for Java Developers
                               Frane                                                                             3
Syntax 1/2
if (condition) {                                  while (condition) {
   instructions;                                    instructions;
} else {                                          }
   instructions;
}                                                 do {
                                                    instructions;
switch (variable) {                               } while (condition);
   case value1 : instructions;
                  break;                          for (start; end-condition; iterator){
   case value2 : instructions;                       instuctions;
                  break;                          }
  default :      instructions;
}                                                 for (var element in object) {
                                                     instructions;
                                                  }
11/06/09             Frane Bandov: JavaScript and AJAX for Java Developers            4
Syntax 2/2
function a (p1, p2, p2) {                       function CompleteClass (parameter)
  instructions;                                 {
  return value;                                   var private_attribute = "private";
}
                                                    this.public_attribute = "public";
var b = function (...) {...}
                                                    var private_method = function () {
                                                      // do something
function MyClass(x){                                };
  this.attribute1 = x;
}                                                   this.public_method = function () {
                                                     // do something public
var object1 = new MyClass(10);                      };
window.alert(object1.attribute1);               }

11/06/09               Frane Bandov: JavaScript and AJAX for Java Developers             5
JavaScript is 100% dynamic
var my_object;

my_object.new_attr = 1; // create new attributes and methods on runtime
// or
my_object["other_new_attr"] = 2;

// and remove them on runtime

delete my_object.other_new_attr;

// or even the whole thing...

delete my_object;


11/06/09              Frane Bandov: JavaScript and AJAX for Java Developers   6
Handling Exceptions
// kind of similar to Java

try {
   // do stuff...
} catch(exception) {
   // ATENTION! JavaScript is untyped and therefore there are no diffrent
// exception types like in Java. You have to distinguish the exceptions by hand
} finally {
  // optional
};

throw("A sample exception"); // throw exceptions



11/06/09              Frane Bandov: JavaScript and AJAX for Java Developers   7
Predefined Objects
 ●   Object: general class, from which all objects are derived
 ●   Function: class for functions
 ●   Array: array-class
 ●   String: string-class
 ●   Boolean: class for boolean values
 ●   Number: class for all kinds of numbers (IEEE 754)
 ●   Math: provides static mathods for mathematical functions
 ●   Date: for date and time operations and values
 ●   RegExp: for regular expressions

11/06/09             Frane Bandov: JavaScript and AJAX for Java Developers   8
Other Helpful Stuff
     window.alert("Hello World");


     var confirmed = window.confirm("Confirm please");
     var answer = window.prompt("Your answer:", "42");


     http://de.selfhtml.org/javascript/objekte/window.htm



11/06/09          Frane Bandov: JavaScript and AJAX for Java Developers   9
DOM – Document Object Model
                 <html>
                 <header>...</header>
                 <body>

                 <img src="some_image.png">
                 <img src="image2.png">
                 <img src="image3.jpg">

                 <script>
                 window.alert(document.images[0].height);
                 if(document.images[1].height >
                 document.images[2].height) window.alert("Image2");
                 else window.alert("Image3");
                 </script>
                 </body>
                 </html>
11/06/09         Frane Bandov: JavaScript and AJAX for Java Developers   10
DOM – more practical (but dirty)
   <html><header>...</header>
   <body>
   <h1 id="my_header">A title</h1>
   <input type="button" onklick="change_header()">

   <script>
   function change_header() {
     document.getElementById("my_header").innerHTML = "My new title";
   }
   </script>
   </body>
   </html>


   http://de.selfhtml.org/javascript/objekte/document.htm
11/06/09             Frane Bandov: JavaScript and AJAX for Java Developers   11
AJAX
     Asynchronous JavaScript And XML




11/06/09        Frane Bandov: JavaScript and AJAX for Java Developers   12
Plain AJAX 1/2
   <script type="text/javascript">
   var xmlHttp = null;
   try {
      xmlHttp = new XMLHttpRequest(); // Mozilla, Opera, Safari, IE7+
   } catch(e) {
      try {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE6
      } catch(e) {
         xmlHttp = null;
      }
   }

   ...


11/06/09            Frane Bandov: JavaScript and AJAX for Java Developers   13
Plain AJAX 2/2
   ...

   if (xmlHttp) {
      xmlHttp.open('GET', 'example.xml', true);
      xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
            alert(xmlHttp.responseText);
         }
      };
      xmlHttp.send(null);
   }

   </script>


11/06/09             Frane Bandov: JavaScript and AJAX for Java Developers   14
Practical AJAX
     e.g. in 'Prototype'

     var myAjax = new Ajax.Request(
       "/server_date.php",
       { method: 'get', onComplete: show_date }
     );

     function show_date( originalRequest ) {
       document.getElementById('output').innerHTML =
     originalRequest.responseText;
     }
11/06/09           Frane Bandov: JavaScript and AJAX for Java Developers   15
AJAX Frameworks
 ●   Prototype: prototypejs.org
 ●   jQuery: jquery.com
 ●   MooTools: mootools.net
 ●   and many more...


 ●   Specially for graphical effects:
      ●    script.aculo.us (needs Prototype)
      ●    Moo.fx

11/06/09              Frane Bandov: JavaScript and AJAX for Java Developers   16
AJAX Frameworks
     Enhancing JavaScript
           i.e. document.getElementById("element_id")
                 → $("element_id")
     Example changing color of an element:
           $("element_id").setStyle({color: '#ff0000'}); // Protoype

           $("#my_div").css("border","3px solid red"); // jQuery
           var div_text_color = $("#my_div").css("color");




11/06/09                Frane Bandov: JavaScript and AJAX for Java Developers   17
Demo



11/06/09   Frane Bandov: JavaScript and AJAX for Java Developers   18
Questions?



11/06/09   Frane Bandov: JavaScript and AJAX for Java Developers   19
Thank You.



11/06/09   Frane Bandov: JavaScript and AJAX for Java Developers   20

Más contenido relacionado

La actualidad más candente

The Case for React.js and ClojureScript
The Case for React.js and ClojureScriptThe Case for React.js and ClojureScript
The Case for React.js and ClojureScriptMurilo Pereira
 
Дмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репортДмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репортSergey Platonov
 
Groovy Grails DevJam Jam Session
Groovy Grails DevJam Jam SessionGroovy Grails DevJam Jam Session
Groovy Grails DevJam Jam SessionMike Hugo
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modulesRafael Winterhalter
 
Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++Sergey Platonov
 
exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...
exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...
exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...tutorialsruby
 
JavaScript: Advanced Scoping & Other Puzzles
JavaScript: Advanced Scoping & Other PuzzlesJavaScript: Advanced Scoping & Other Puzzles
JavaScript: Advanced Scoping & Other PuzzlesSencha
 
Android Nâng cao-Bài 9-Debug in Android Application Development
Android Nâng cao-Bài 9-Debug in Android Application Development Android Nâng cao-Bài 9-Debug in Android Application Development
Android Nâng cao-Bài 9-Debug in Android Application Development Phuoc Nguyen
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & WebkitQT-day
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming LanguagesYudong Li
 
Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016Danny Preussler
 
Making Exceptions on Exception Handling (WEH 2012 Keynote Speech)
Making Exceptions on  Exception Handling (WEH 2012 Keynote Speech)Making Exceptions on  Exception Handling (WEH 2012 Keynote Speech)
Making Exceptions on Exception Handling (WEH 2012 Keynote Speech)Tao Xie
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontrackingvamsi krishna
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydneyjulien.ponge
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingAnton Arhipov
 
Pure Java RAD and Scaffolding Tools Race
Pure Java RAD and Scaffolding Tools RacePure Java RAD and Scaffolding Tools Race
Pure Java RAD and Scaffolding Tools RaceBaruch Sadogursky
 
How Does Kubernetes Build OpenAPI Specifications?
How Does Kubernetes Build OpenAPI Specifications?How Does Kubernetes Build OpenAPI Specifications?
How Does Kubernetes Build OpenAPI Specifications?reallavalamp
 

La actualidad más candente (20)

The Case for React.js and ClojureScript
The Case for React.js and ClojureScriptThe Case for React.js and ClojureScript
The Case for React.js and ClojureScript
 
Дмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репортДмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репорт
 
Qt Rest Server
Qt Rest ServerQt Rest Server
Qt Rest Server
 
Groovy Grails DevJam Jam Session
Groovy Grails DevJam Jam SessionGroovy Grails DevJam Jam Session
Groovy Grails DevJam Jam Session
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modules
 
Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++
 
exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...
exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...
exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-bas...
 
JavaScript: Advanced Scoping & Other Puzzles
JavaScript: Advanced Scoping & Other PuzzlesJavaScript: Advanced Scoping & Other Puzzles
JavaScript: Advanced Scoping & Other Puzzles
 
Rx java in action
Rx java in actionRx java in action
Rx java in action
 
Android Nâng cao-Bài 9-Debug in Android Application Development
Android Nâng cao-Bài 9-Debug in Android Application Development Android Nâng cao-Bài 9-Debug in Android Application Development
Android Nâng cao-Bài 9-Debug in Android Application Development
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & Webkit
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
 
Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016
 
Making Exceptions on Exception Handling (WEH 2012 Keynote Speech)
Making Exceptions on  Exception Handling (WEH 2012 Keynote Speech)Making Exceptions on  Exception Handling (WEH 2012 Keynote Speech)
Making Exceptions on Exception Handling (WEH 2012 Keynote Speech)
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
 
Kubernetes
KubernetesKubernetes
Kubernetes
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
Pure Java RAD and Scaffolding Tools Race
Pure Java RAD and Scaffolding Tools RacePure Java RAD and Scaffolding Tools Race
Pure Java RAD and Scaffolding Tools Race
 
How Does Kubernetes Build OpenAPI Specifications?
How Does Kubernetes Build OpenAPI Specifications?How Does Kubernetes Build OpenAPI Specifications?
How Does Kubernetes Build OpenAPI Specifications?
 

Destacado

Planning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsPlanning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsChristian Heilmann
 
Good For Nothing UXD Update - General Assembly Project
Good For Nothing UXD Update - General Assembly ProjectGood For Nothing UXD Update - General Assembly Project
Good For Nothing UXD Update - General Assembly ProjectCraig Adams
 
Enabling Self Service Business Intelligence using Excel
Enabling Self Service Business Intelligenceusing ExcelEnabling Self Service Business Intelligenceusing Excel
Enabling Self Service Business Intelligence using ExcelAlan Koo
 
An Advertising Design Problem
An Advertising Design ProblemAn Advertising Design Problem
An Advertising Design ProblemCraig Adams
 

Destacado (6)

Planning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsPlanning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teams
 
Why altmetrics?
Why altmetrics?Why altmetrics?
Why altmetrics?
 
Good For Nothing UXD Update - General Assembly Project
Good For Nothing UXD Update - General Assembly ProjectGood For Nothing UXD Update - General Assembly Project
Good For Nothing UXD Update - General Assembly Project
 
UX Techniques
UX TechniquesUX Techniques
UX Techniques
 
Enabling Self Service Business Intelligence using Excel
Enabling Self Service Business Intelligenceusing ExcelEnabling Self Service Business Intelligenceusing Excel
Enabling Self Service Business Intelligence using Excel
 
An Advertising Design Problem
An Advertising Design ProblemAn Advertising Design Problem
An Advertising Design Problem
 

Similar a JavaScript and AJAX Guide for Java Devs

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
Intro to ES6 and why should you bother !
Intro to ES6 and why should you bother !Intro to ES6 and why should you bother !
Intro to ES6 and why should you bother !Gaurav Behere
 
Ob1k presentation at Java.IL
Ob1k presentation at Java.ILOb1k presentation at Java.IL
Ob1k presentation at Java.ILEran Harel
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGuillaume Laforge
 
A few good JavaScript development tools
A few good JavaScript development toolsA few good JavaScript development tools
A few good JavaScript development toolsSimon Kim
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationAjax Experience 2009
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA PresentationRob Tweed
 

Similar a JavaScript and AJAX Guide for Java Devs (20)

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
oojs
oojsoojs
oojs
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Intro to ES6 and why should you bother !
Intro to ES6 and why should you bother !Intro to ES6 and why should you bother !
Intro to ES6 and why should you bother !
 
Ob1k presentation at Java.IL
Ob1k presentation at Java.ILOb1k presentation at Java.IL
Ob1k presentation at Java.IL
 
Training javascript 2012 hcmut
Training javascript 2012 hcmutTraining javascript 2012 hcmut
Training javascript 2012 hcmut
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
Prototype-1
Prototype-1Prototype-1
Prototype-1
 
Prototype-1
Prototype-1Prototype-1
Prototype-1
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
 
backend
backendbackend
backend
 
backend
backendbackend
backend
 
GlassFish v3 : En Route Java EE 6
GlassFish v3 : En Route Java EE 6GlassFish v3 : En Route Java EE 6
GlassFish v3 : En Route Java EE 6
 
JavaScript
JavaScriptJavaScript
JavaScript
 
A few good JavaScript development tools
A few good JavaScript development toolsA few good JavaScript development tools
A few good JavaScript development tools
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
 
Modern frontend in react.js
Modern frontend in react.jsModern frontend in react.js
Modern frontend in react.js
 

JavaScript and AJAX Guide for Java Devs

  • 1. JavaScript and AJAX by Frane Bandov
  • 2. JavaScript ≠ Java ● Developed in 1995/96 by Netscape ● No technological relation to Java ● Originally named LiveScript, but renamed to JavaScript for marketing purposes (Java hype in the mid-90s) ● Standardized as ECMAScript (ECMA-262) in 1998 ● Very small object and instruction set → efficient ● Runs in a sandbox ● Manipulation of HTML documents via DOM 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 2
  • 3. JavaScript History source: Wikipedia Version Release Description Netscape Firefox Internet Opera Safari Chrome date Exporer 1.0 03.1996 2.0 3.0 1.1 08.1996 3.0 1.2 06.1997 4.0-4.05 1.3 10.1998 ECMA-262 1st edition / 4.06- ECMA-262 2nd edition 4.7x 1.4 Server 1.5 11.2000 ECMA-262 3rd edition 6.0 1.0 5, 6, 7, 8 6-9 1.6 11.2005 1.5 + Array extras + Array 1.5 3.0, & String generics + E4X 3.1 1.7 10.2006 1.6 + Pythonic generators 2.0 2.2, 1.0 + Iterators + let 4.0 1.8 06.2008 1.7 + Generator 3.0 expressions + Expression closures 1.8.1 1.8 + geringfügige 3.5 Updates 1.9 1.8.1 + ECMAScript 5 4.0 11/06/09 Compliance Bandov: JavaScript and AJAX for Java Developers Frane 3
  • 4. Syntax 1/2 if (condition) { while (condition) { instructions; instructions; } else { } instructions; } do { instructions; switch (variable) { } while (condition); case value1 : instructions; break; for (start; end-condition; iterator){ case value2 : instructions; instuctions; break; } default : instructions; } for (var element in object) { instructions; } 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 4
  • 5. Syntax 2/2 function a (p1, p2, p2) { function CompleteClass (parameter) instructions; { return value; var private_attribute = "private"; } this.public_attribute = "public"; var b = function (...) {...} var private_method = function () { // do something function MyClass(x){ }; this.attribute1 = x; } this.public_method = function () { // do something public var object1 = new MyClass(10); }; window.alert(object1.attribute1); } 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 5
  • 6. JavaScript is 100% dynamic var my_object; my_object.new_attr = 1; // create new attributes and methods on runtime // or my_object["other_new_attr"] = 2; // and remove them on runtime delete my_object.other_new_attr; // or even the whole thing... delete my_object; 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 6
  • 7. Handling Exceptions // kind of similar to Java try { // do stuff... } catch(exception) { // ATENTION! JavaScript is untyped and therefore there are no diffrent // exception types like in Java. You have to distinguish the exceptions by hand } finally { // optional }; throw("A sample exception"); // throw exceptions 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 7
  • 8. Predefined Objects ● Object: general class, from which all objects are derived ● Function: class for functions ● Array: array-class ● String: string-class ● Boolean: class for boolean values ● Number: class for all kinds of numbers (IEEE 754) ● Math: provides static mathods for mathematical functions ● Date: for date and time operations and values ● RegExp: for regular expressions 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 8
  • 9. Other Helpful Stuff window.alert("Hello World"); var confirmed = window.confirm("Confirm please"); var answer = window.prompt("Your answer:", "42"); http://de.selfhtml.org/javascript/objekte/window.htm 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 9
  • 10. DOM – Document Object Model <html> <header>...</header> <body> <img src="some_image.png"> <img src="image2.png"> <img src="image3.jpg"> <script> window.alert(document.images[0].height); if(document.images[1].height > document.images[2].height) window.alert("Image2"); else window.alert("Image3"); </script> </body> </html> 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 10
  • 11. DOM – more practical (but dirty) <html><header>...</header> <body> <h1 id="my_header">A title</h1> <input type="button" onklick="change_header()"> <script> function change_header() { document.getElementById("my_header").innerHTML = "My new title"; } </script> </body> </html> http://de.selfhtml.org/javascript/objekte/document.htm 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 11
  • 12. AJAX Asynchronous JavaScript And XML 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 12
  • 13. Plain AJAX 1/2 <script type="text/javascript"> var xmlHttp = null; try { xmlHttp = new XMLHttpRequest(); // Mozilla, Opera, Safari, IE7+ } catch(e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE6 } catch(e) { xmlHttp = null; } } ... 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 13
  • 14. Plain AJAX 2/2 ... if (xmlHttp) { xmlHttp.open('GET', 'example.xml', true); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4) { alert(xmlHttp.responseText); } }; xmlHttp.send(null); } </script> 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 14
  • 15. Practical AJAX e.g. in 'Prototype' var myAjax = new Ajax.Request( "/server_date.php", { method: 'get', onComplete: show_date } ); function show_date( originalRequest ) { document.getElementById('output').innerHTML = originalRequest.responseText; } 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 15
  • 16. AJAX Frameworks ● Prototype: prototypejs.org ● jQuery: jquery.com ● MooTools: mootools.net ● and many more... ● Specially for graphical effects: ● script.aculo.us (needs Prototype) ● Moo.fx 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 16
  • 17. AJAX Frameworks Enhancing JavaScript i.e. document.getElementById("element_id") → $("element_id") Example changing color of an element: $("element_id").setStyle({color: '#ff0000'}); // Protoype $("#my_div").css("border","3px solid red"); // jQuery var div_text_color = $("#my_div").css("color"); 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 17
  • 18. Demo 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 18
  • 19. Questions? 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 19
  • 20. Thank You. 11/06/09 Frane Bandov: JavaScript and AJAX for Java Developers 20