1 JavaScript & AJAX CS 236607, Winter 2007/8. 2 JavaScript.

Slides:



Advertisements
Similar presentations
AJAX Asynchronous JavaScript and XML. AJAX An interface that allows for the HTTP communication without page refreshment Web pages are loaded into an object.
Advertisements

1 AJAX – Asynchronous JavaScript and XML – Part II CS , Spring 2010.
© Copyright 2008 STI - INNSBRUCK Web Engineering Web Technologies III Lecture XI – 16 th December 2008 Federico M. Facca.
9. AJAX & RIA. 2 Motto: O! call back yesterday, bid time return. — William Shakespeare.
AJAX Presented by: Dickson Fu Dimas Ariawan Niels Andreassen Ryan Dial Jordan Nielson CMPUT 410 University of Alberta 2006.
19-Jun-15 Ajax. The hype Ajax (sometimes capitalized as AJAX) stands for Asynchronous JavaScript And XML Ajax is a technique for creating “better, faster,
WHAT IS AJAX? Zack Sheppard [zts2101] WHIM April 19, 2011.
Cloud Computing Lecture #7 Introduction to Ajax Jimmy Lin The iSchool University of Maryland Wednesday, October 15, 2008 This work is licensed under a.
1 JavaScript & AJAX – Part II CS , Spring 2008.
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
1 JavaScript & AJAX CS , Spring JavaScript.
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
Introduction to AJAX AJAX Keywords: JavaScript and XML
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
JavaScript & jQuery the missing manual Chapter 11
CS 4720 RESTfulness and AJAX CS 4720 – Web & Mobile Systems.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
ASP.NET + Ajax Jesper Tørresø ITNET2 F08. Ajax Ajax (Asynchronous JavaScript and XML) A group of interrelated web development techniques used for creating.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Intro to Ajax Fred Stluka Jan 25, /25/2006Intro to AjaxFred Stluka2 What is Ajax? "Asynchronous JavaScript and XML" New name for an old technique:
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Instructor, Dr. Khalili Bahram Jeevan Kumar Gogineni.
Ajax. –Asynchronous JavaScript and XML –Umbrella term for technologies that often: Use client-side scripting for layout and formatting Use less than full.
Chapter 6 Server-side Programming: Java Servlets
AJAX (also known as: XMLHTTP, Remote Scripting, XMLHttpRequest, etc.) Matt Warden.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
AJAX Compiled from “AJAX Programming” [Sang Shin] (Asynchronous JavaScript and XML)
Ajax. –Asynchronous JavaScript and XML –Umbrella term for technologies that often: Use client-side scripting for layout and formatting Use less than full.
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
Web Technology Introduction AJAXAJAX. AJAX Outline  What is AJAX?  Benefits  Real world examples  How it works  Code review  Samples.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
the acronym for Asynchronous JavaScript and XML.
Ajax for Dynamic Web Development Gregory McChesney.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
 AJAX – Asynchronous JavaScript and XML  Ajax is used to develop fast dynamic web applications  Allows web pages to be updated asynchronously by transferring.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
AJAX Use Cases for WSRP Subbu Allamaraju BEA Systems Inc WSRP F2F Meeting, May 2006.
AJAX – Asynchronous JavaScript And XML By Kranthi Kiran Nuthi CIS 764 Kansas State University.
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
 AJAX technology  Rich User Experience  Characteristics  Real live examples  JavaScript and AJAX  Web application workflow model – synchronous vs.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
PHP and AJAX. Servers and Clients For many years we tried to move as much as possible to the server. Weak clients, poor bandwidth, browser compatibility..
JavaScript, Sixth Edition Chapter 11 Updating Web Pages with Ajax.
JavaScript and Ajax Week 10 Web site:
CITA 330 Section 10 Web Remoting Techniques. Web Remoting Web Remoting is a term used to categorize the technique of using JavaScript to directly make.
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
Open Solutions for a Changing World™ Eddy Kleinjan Copyright 2005, Data Access WordwideNew Techniques for Building Web Applications June 6-9, 2005 Key.
Ajax & Client-side Dynamic Web Gunwoo Park (Undergraduate)
JavaScript and Ajax (Ajax Tutorial)
CSE 154 Lecture 11: AJAx.
Not a Language but a series of techniques
Subbu Allamaraju BEA Systems Inc
AJAX – Asynchronous JavaScript and XML
AJAX.
CSE 154 Lecture 11: AJAx.
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
ISC440: Web Programming 2 AJAX
AJAX CS-422 Dick Steflik.
Intro to Ajax Fred Stluka Jan 25, 2006.
AJAX By Prof. B.A.Khivsara
Presentation transcript:

1 JavaScript & AJAX CS , Winter 2007/8

2 JavaScript

3 Overview JavaScript is a scripting language most often used for client-side web development, and best known for this use in websites (as client-side JavaScript). JavaScript is essentially unrelated to the Java programming language, though it copies many Java names and naming conventions. JavaScript is used in many Web pages to add functionality, validate forms, detect browsers, and much more.

4 JavaScript by Examples We will look at some JavaScript examples…

5 Hello World document.write(“ Hello World! "); DOM treatment of the page

6 Document URL The URL of this document is: document.write(document.URL);

7 More Examples Count the number of images in a document What are the coordinates of the cursor?  Notice events are thrown (events-driven)

Form Validation function validate() …(next slide) Name (max 10 chararcters): Age (from 1 to 100):

Form Validation (Cont.) function validate() { var var age=document.getElementById("age").value; var fname=document.getElementById("fname").value; submitOK="true"; if (fname.length>10){ alert("The name must be less than 10 characters"); submitOK="false"; } if (isNaN(age)||age 100) { alert("The age must be a number between 1 and 100"); submitOK="false"; } if (at==-1) { alert("Not a valid !"); submitOK="false"; } if (submitOK=="false") { return false; } } DOM Objects JavaScript Function Object Property

10 AJAX

11 Where Were We Before AJAX? Static pages give the illusion of interactivity through standard form submissions. Form submissions result in full page loads.

12 So, What’s The Problem? Many actions only manipulate small portions of the page but the entire page must be reloaded. Server responses contain the entire page content rather than just the portion being updated. Loading entire pages typically results in several additional HTTP requests for images, style sheets, scripts, and any other content that may be on the page.

13 AJAX - Asynchronous JavaScript and XML An interface that allows for the HTTP communication without page refreshment. Web pages are loaded into an object within the script (e.g., JavaScript) execution and integrated with the page content. Thus, the Web page can communicate with the server without refreshing the whole page.

14 Real-Life Examples of AJAX Apps Google maps  Goolgle Suggest  Gmail  Yahoo Maps (new)  Many more…

15 AJAX Components JavaScript  DOM XMLHttpRequest object ( XHR ) XML

16 Ajax Fundamentals Ajax uses a three-step process: 1.Request a URL from JavaScript code on the client. 2.Handle the URL on the server and write to the response. 3.After the response is complete, integrate the response into the DOM (Document Object Model).  In an Ajax request we don't refresh the entire page; instead, we update only part of the page.

17 The Server side Did we reduce the load on the server? Ajax newcomers sometimes mistakenly believe that Ajax, because it provides a more responsive user interface, reduces server-side traffic. In fact, Ajax applications typically have more server-side traffic because each Ajax request involves a trip to the server.  Because those requests are asynchronous, however, Ajax creates the perception of a more responsive UI, though it typically does not reduce the load on the server.

18 So, How Does It Work? JavaScript is used to:  Create and control instances of the XMLHttpRequest (XHR) object.  Provide handlers for responses.  Manipulate the DOM. The XMLHttpRequest object:  Allows scripts to perform HTTP client functionality.  Supports GET and POST operations.

19 Launching HTTP Requests Typically, 3 steps are required: Construct and configure an XMLHttpRequest object Launch the request Process the response

20 Constructing an XMLHttpRequest For Mozilla: For Microsoft Explorer: var request = new XMLHttpRequest(); var request = new ActiveXObject("Microsoft.XMLHTTP");

21 Configuring an XMLHttpRequest request.open("method","URL",false) request.setRequestHeader("header","value") method is GET, POST, etc. URL must be in the domain of the current (or a relative URL), for security reasons The false will be discussed later

22 Launching the Request request.send(content ) content is the posted in a POST request content can be "null" or empty

Reading the Response request.responseText The response as flat text request.responseXML The response as a (DOM) Document object Available if response Content-Type is text/XML request.statusrequest.statusText request.getAllResponseHeaders() request.getResponseHeader("header")

24 Jokes... 2 slides ahead... An Example

Select a Joke: Joke 1 Joke 2 Joke 3 An Example (Cont.)

var jDiv; function init() { jDiv = document.getElementById("jokediv");} function setJoke(value) { request = new XMLHttpRequest(); request.open("GET","joke"+value+".txt",false); request.send(null); if(request.status==200){ jDiv.innerHTML=request.responseText; } else {jDiv.innerHTML = " Cannot load joke... ";} } What if we didn’t get yet the response in this stage?

27 Example (Cont.) Our examples use “false" in the third parameter of open().  This parameter specifies whether the request should be handled asynchronously. True means that the script continues to run after the send() method, without waiting for a response from the server. Let’s see how it works, and how it is saved on the Tomcat server.

28 Asynchronous Requests Reading of a Web page can take a long time during which the browser is blocked Solution: launch the request asynchronously That is, the execution continues after send is called without waiting for it to complete When the request is completed, a predefined function is called request.open("method","URL",true)

29 XMLHttpRequest States The XMLHttpRequest goes through several states: In the request configuration, you can define a function to call upon state change: 0 0 not initialized 1 1 loading 2 2 loaded 3 3 interactive 4 4 complete request.onreadystatechange = functionName

30 XMLHttpRequest States (Cont.) Use request.readyState to get the current state of the request Use request.abort() to stop the request

31 var request; function setJoke(value) { request = new XMLHttpRequest(); request.open("GET","joke"+value+".txt",true); request.onreadystatechange = updateJokeDiv; request.send(null); } Asynchronous Example

function updateJokeDiv() { if(request.readyState<4) { jokeDiv.innerHTML = " Loading... "; return; } if(request.status==200) { jokeDiv.innerHTML = request.responseText; } else { jokeDiv.innerHTML = " Cannot load joke! "; } } An Example (Cont.)

33 Integrating AJAX and XML using DOM The next example shows how XML data can be parsed and added into the content of your page

34 Country List … Select a Continent: XML+AJAX Example

Asia Africa Europe XML+AJAX Example (Cont.)

function init() { continents = document.getElementById("continenetList"); countries = document.getElementById("countryList"); } function loadCountries() { var xmlURL = "countries-"+continents.value+".xml"; var request = new XMLHttpRequest(); request.onreadystatechange = updateCountries () ; request.open("GET",xmlURL,true); request.send(null); } XML+AJAX Example (Cont.)

function updateCountries() { if(request.readyState==4) { while(countries.length>0){countries.remove(0);} if(request.status==200) { var names = request.responseXML.getElementsByTagName("name"); for(var i=0; i<names.length; ++i) { option = document.createElement("option"); option.text=option.value=names[i].firstChild.nodeValue; countries.appendChild(option);} }}} XML+AJAX Example (Cont.)

38 JavaScript Libraries To reduce the amount of JavaScript code you need to write for Ajax requests, and to make sure that those requests succeed across multiple browsers, one better use a JavaScript library that neatly encapsulates those details and sharp edges in convenient JavaScript objects. The Prototype Library is one option. The Prototype Library

39 Resources DaveFancher.com Hebrew University Course javapassion.com W3 Schools Wikipedia Core JavaServer Faces(2nd Edition) / David Geary, Cay S. Horstmann