170 likes | 313 Vues
This overview of AJAX (Asynchronous JavaScript and XML) delves into why this technology is essential for modern web applications. Learn how AJAX enhances user experience by allowing smaller, faster, and more interactive applications. We cover key components including XMLHttpRequest, various AJAX frameworks like Google Web Toolkit, and best practices for implementing AJAX in your projects. Whether you’re validating forms, implementing auto-complete features, or optimizing server communication, this guide provides foundational knowledge and practical examples to harness the power of AJAX effectively.
E N D
AJAX: Web Programming's Toy Fawaz Ghali F.Ghali@Warwick.ac.uk
Overview • Why AJAX • Getting started • AJAX Frameworks • Demo
AJAX • Asynchronous JavaScript and XML • JavaScript communicates with the server (XMLHttpRequest) • Asynchronous data transfer • Web applications are smaller, faster and more user-friendly. • Usually, you still need server programming language (i.e., PHP)
AJAX components • HTML/CSS: presenting information • Document Object Model (DOM): dynamic interaction with the information • XMLHttpRequest object: retrieving data from the server asynchronously • JavaScript: wrap AJAX code
AJAX use • Forms validations • Auto-complete / Auto-suggest • Master-detail relations (more information to display) • User-friendly screens • Web 2.0 applications
How does AJAX work? Source: http://java.sun.com/javaee/javaserverfaces/ajax/tutorial.jsp
AJAX basic example <html> <head> <script src="prototype.js" type="text/javascript"></script> <script type="text/javascript"> function ajaxUpdater(id,url) { new Ajax.Updater(id,url,{asynchronous:true}); } </script> </head> prototype.js is AJAX library : http://www.prototypejs.org/
AJAX basic example cont HTML code: <body> <div id="updateme"></div> <input type="button" value="Update" onClick="ajaxUpdater('updateme',‘hello.php')"> </body> </html>
AJAX basic example Cont hello.php: <?php echo "Hello world!"; // you can define any function ?> http://prolearn.dcs.warwick.ac.uk/AH-RO-2008/ajax_demos/ajax_1.html
AJAX Frameworks • AJAX frameworks simplify the code and speed the development. • Depend on server programming language • Component (widgets) use vs. direct use • Mashups with Google, Yahoo!, Flickr etc…
How to choose framework? • Server independent or not? • Structural JavaScript? • Re-usability of your AJAX code? • Documentation (examples, support) • Learning curve
Google Web Toolkit (GWT) • Faster AJAX than you can write by hand • Smaller, more compact, cacheable code • Support IE, Firefox, Mozilla, Safari • Browser's "back" button correctly use • Full-featured debugging • Unit tests (based on JUnit)
GWT <html> <head> <title>Hello</title> </head> <body> <script language="javascript" src="com.google.gwt.sample.hello.Hello.nocache.js"></script> </body> </html>
GWT public class Hello implements EntryPoint { public void onModuleLoad() { Button b = new Button("Click me", new ClickListener() { public void onClick(Widget sender) { Window.alert("Hello, AJAX"); } }); RootPanel.get().add(b); } }
GWT • Demo: http://gwt.google.com/samples/Hello/Hello.html
AJAX Examples & Tutorials • http://www.w3schools.com/Ajax/ • http://www.ajaxtutorial.net/ • http://www.tizag.com/ajaxTutorial/ • http://java.sun.com/javaee/javaserverfaces/ajax/tutorial.jsp
AJAX best practices • Tell the user that the websites uses AJAX (i.e., dynamic update) • Provide non-AJAX options • Provide alerts for dynamic changes • Make navigation easy • Update HTML elements with new content rather creating new elements