27
Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs AJAX: Rich Internet Applications Web Programming Uta Priss ZELL, Ostfalia University 2013 Web Programming AJAX Slide 1/27

AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

AJAX: Rich Internet Applications

Web Programming

Uta PrissZELL, Ostfalia University

2013

Web Programming AJAX Slide 1/27

Page 2: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Outline

Rich Internet Applications

AJAX

AJAX example

Conclusion

More AJAX

Search engine APIs

Web Programming AJAX Slide 2/27

Page 3: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Rich Internet Applications

I A combination of desktop and web applications.

I Easy to install on the client (just needs browser).

I Client engine; “fat” client.

I Operating system neutral.

I Asynchronous communication (reload without refresh).

I Reduced server-client communication.

I Might even work off-line.

(The term “Rich Internet Applications” was coined by Macromediain 2002.)

Web Programming AJAX Slide 3/27

Page 4: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Examples

I Google Maps: adjacent maps are “pre-fetched”.

I Microsoft Silverlight: programmable plugin for graphics,audio, video.

I Curl: web programming language uses Curl runtimeenvironment plugin (not to be confused with cURL).

I Adobe Flex: based on Flash and J2EE.

I JavaFX (Sun Microsystems).

I Google Web Toolkit: produces AJAX applications from Javacode.

I Browser-based “operating systems” (EyeOS, G.ho.st,, etc).

Web Programming AJAX Slide 4/27

Page 5: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Older similar technologies

I Remote Scripting (Microsoft, since 1998)

I Rich web applications/clients

I Java Applets

I DHTML: HTML + JavaScript + CSS + DOM;changes content after the page has loaded;eg. rollover buttons, drop-down menus

I Adobe Flash

Web Programming AJAX Slide 5/27

Page 6: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Shortcomings of Rich Internet Applications

I Network traffic can increase if too much data is pre-fetched.

I Initial download time can be long.

I Requires JavaScript (might be disabled, might be slow).

I “Sandboxing”: incomplete access to system resources.

I Traditional http-based network monitoring techniques don’twork, because of the asynchronous requests.

Web Programming AJAX Slide 6/27

Page 7: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

What is AJAX

AJAX: Asynchronous JavaScript And XML

I Used for creating interactive web applications or rich Internetapplications.

I Update pages “on the fly”.

I Retrieve data from the server asynchronously in thebackground.

The term AJAX was coined by Jesse J. Garrett in 2005.

Possible since 1997 (IFrame in IE and Layers in Netscape).

Web Programming AJAX Slide 7/27

Page 8: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

What is AJAX NOT

AJAX is not ...

I a programming language;

I a server technology.

Because it uses standard programming languages and browsertechnologies.

Web Programming AJAX Slide 8/27

Page 9: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Technologies used

I XHTML and CSS for presentation

I DOM for dynamic display of and interaction with data

I XML and XSLT or JSON etc for interchange andmanipulation of data

I XMLHttpRequest object or IFrames etc for asynchronouscommunication

I JavaScript or VBScript etc to bring these technologiestogether

Web Programming AJAX Slide 9/27

Page 10: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

How does it work:

I User triggers an HTML event, such as onClick oronMouseOver.

I JavaScript sends HTTP request to server.

I Server supplies a file (XML, HTML or text) as normal.

I JavaScript in the current page selects part of the file fordisplay.

I JavaScript statement displays the selected part.

Web Programming AJAX Slide 10/27

Page 11: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Pros and cons of AJAX

Pros:

I Increase speed, reduce bandwidth.

I More interactivity.

I More complex applications (e.g. email clients)

Cons:

I Browser’s “back” button and bookmarking may not work.

I Not indexed by search engines.

I Accessibility issues: people who use screen readers and otherspecialist browsers may not be able to access the content.

I Will not work if JavaScript disabled.

I Even more possibilities for errors, browser incompatibility etc.

Web Programming AJAX Slide 11/27

Page 12: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

The XMLHttpRequest (XHR) API

For example:

var request = new XMLHttpRequest()request.open("GET",url,true)request.send(null)request.onreadystatechange=stateChanged()function stateChanged() {

if (request.readyState == 4) {alert(request.responseText)

}}

Web Programming AJAX Slide 12/27

Page 13: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

The XMLHttpRequest Methods

I request = new XMLHttpRequest() : defines new requestobject

I request.open(”GET”,url,true) : “true” means asynchronous,don’t wait for “send”

I request.send(null) : send the request

I request.onreadystatechange : defines the event handler

I request.readyState : “4” means finished

I request.responseText : the response as a string

I request.responseXML : the response as XML

Web Programming AJAX Slide 13/27

Page 14: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Combining with DOM

If the response is returned as XML, DOM can be used to extractcontent:

xmldoc = request.responseXML;node = xmldoc.getElementsByTagName(’...’).item(0);alert(node.firstChild.data);

Web Programming AJAX Slide 14/27

Page 15: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

What can JavaScript do with the content?

This code shows how “replace this” is replaced by “value” whenthe user clicks (without refreshing the page):

<span id=’n’>replace this</span>onclick="document.getElementById(’n’).innerHTML=’value’"

For AJAX, the “document.getElementById ...” can be placed intothe onreadystatechange Event Handler.

Web Programming AJAX Slide 15/27

Page 16: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Other means of sending content

Apart from sending content as XML, it can also be sent usingJSON (JavaScript Object Notation):

{"Name": "John Doe","Age": "25","address": {

"streetAddress": "10 Colinton Road","postalCode": "EH10 5DT","city": "Edinburgh"}

}

Web Programming AJAX Slide 16/27

Page 17: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

XML versus JSON

XML has more features (validation, many tools and predefinedformats).

JSON is simpler, smaller, easier to parse. It is supported by otherlanguages (for example, PHP) as well.

Web Programming AJAX Slide 17/27

Page 18: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Security: Server-side

Transmitting data between client and server always implies securityrisks!

On the server-side: this is mostly the normal security problem. Theusual checks for POST/GET data and Query Strings must beperformed. If databases are involved, then the requests need to bechecked for SQL injection and so on.

Web Programming AJAX Slide 18/27

Page 19: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Security: Client-side

On the client-side: this depends on the security of JavaScript.Using “eval()” for parsing JSON is dangerous. There is a dangerof cross-site request forgery, etc. If third party advertisements areinserted in a page, then no single developer is in charge of the code.

Unfortunately, the user is not in control of the code. The user onlyhas a choice of turning JavaScript on or off and making sure thatthe latest browser version is installed.

Web Programming AJAX Slide 19/27

Page 20: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Summary:

I JavaScript allows replacing content without refreshing thepage (for example, using the <span> tag) .

I The XMLHttpRequest API facilitates retrieving content fromserver-side files or remote websites.

I The XMLHttpRequest methods can run in separate threads,without stopping the script ⇒ asynchronous requests.

I The requested website can be a server-side script which mightaccess databases or other complex functionality.

I Complex content can be sent as XML (processed by DOM) oras JSON.

Web Programming AJAX Slide 20/27

Page 21: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Javascript development tools

In order to develop and debug complicated Javascript (includingremote scripts, AJAX, etc), one should use tools:

I Firefox: Firebug, Web Developer Extension

I Internet Explorer Developer Toolbar,Visual Web Developer 2008 Express Edition, Visual Studio

I Safari: Web Inspector

I Opera: Dragonfly

Web Programming AJAX Slide 21/27

Page 22: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Remote content

The XMLHttpRequest API allows to include remote content inJavascript. This uses the HTTP protocol and is used for data.

Other possibilities: include remote Javascript code (see next slide)or use IFrame.

Web Programming AJAX Slide 22/27

Page 23: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Including a remote script

<script src=’http://servername/username/scriptinclude’type=’text/javascript’></script>

The file ’scriptinclude’ contains Javascript functions etc, but nohtml and no <script> tags.

Web Programming AJAX Slide 23/27

Page 24: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Security: cross-site scripting attacks

I In PHP, htmlentities() should be applied to all requestedcontent.

I Including content via XMLHttpRequest: remote site couldpotentially request local page, which is outside sandbox(especially for older IE versions).

I If malicious URL is embedded in a genuine page and the userclicks on the malicious URL, a script from the malicious pagecould run as if it came from the genuine page.

Web Programming AJAX Slide 24/27

Page 25: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Defence: same origin policy (SOP)

Scripts from one IP prevented from accessing data/properties ofdocuments from different IP.

But, this does not apply to scripts loaded via <script src= ...>which are treated as same origin as the loading page.

(Mashups often use <script src= ...>.)

Web Programming AJAX Slide 25/27

Page 26: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Search engine APIs

I In order to issue search engine queries from within a program,an API is needed.

I The program can then display the results in a custom manneror it can use the results to issue more queries.

I Google’s API used to be based on SOAP. But the currentversion is using AJAX.

I Other search engines have similar APIs. For example, Yahoohas APIs mostly using REST.

Web Programming AJAX Slide 26/27

Page 27: AJAX: Rich Internet Applications · 2021. 7. 6. · Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs Rich Internet Applications I A combination

Rich Internet Applications AJAX AJAX example Conclusion More AJAX Search engine APIs

Google’s API

<script src=’http://www.google.com/jsapi’ type=’text/javascript’></script>...google.load(’search’, ’1.0’);function OnLoad() {var searchControl = new google.search.SearchControl();searchControl.addSearcher(new google.search.WebSearch());searchControl.draw(document.getElementById(’searchcontrol’));searchControl.execute(’VW GTI’);}google.setOnLoadCallback(OnLoad, true);...<body><div id=’searchcontrol’>Loading</div></body>

Web Programming AJAX Slide 27/27