SlideShare uma empresa Scribd logo
1 de 39
AJAX Workshop
Karen A. Coombs
University of Houston Libraries
Jason A. Clark
Montana State University Libraries
Outline
1. What you’re in for…
2. What’s AJAX?
3. Why AJAX?
4. Look at some AJAX examples
5. AJAX for Libraries
6. Walkthrough sample AJAX application
What you’re in for…
• A discussion about an emerging
web application framework
• An introduction to the essential
elements of AJAX
• How does AJAX fit into libraries?
• Walkthrough the code of a working
AJAX application
What is AJAX?
• Asynchronous Javascript and XML
– Not all AJAX apps involve XML
• Combination of technologies
– XHTML, CSS, DOM
– XML, XSLT, XMLHttp, JavaScript
– Some server scripting language
• A method for building more responsive
and interactive applications
AJAX Components
XHTML and CSS
Ajax applies these familiar Web standards for styling the look and feel of
a page and to markup those areas on a page that will be targeted for
data updates.
DOM (document object model)
Ajax uses the DOM to manipulate dynamic page views for data and to
walkthrough documents to “cherrypick” data. The DOM enables certain
pieces of an Ajax page to be transformed and updated with data.
XML, JSON (Javascript Object Notation), HTML, or plain text
Ajax can use any of these standards to provide structure to the data it
passes to and from a page.
XMLHttpRequest object
The heavy lifter for Ajax: It’s a javascript object embedded in most
modern browsers that sets up data request/response pipelines between
client and server.
Javascript
Lightweight programming language that Ajax uses for instructions to
bind all of the components together.
Why AJAX?
• Want to make your applications
more interactive
• Want to incorporate data from
external Web Services
• Don’t want your users to have to
download a plugin
• Client scripting
– Web browser does all the work
• Server Scripting
– Web server does all the work
• AJAX leverages both client and
server side scripting
How AJAX Works
AJAX Web Interaction
• What you don’t see
• Data reload happens in the
background
• JavaScript queries the server to get
the proper data without you knowing
it
• Page updates without a screen
“reload”
Potential Problems
• Javascript MUST be enabled
• Back button doesn’t always work
• Pages can be difficult to bookmark
• Search engines may not be able to
index all portions of an AJAX site
• Cross browser differences in how
XML is dealt with
Some AJAX examples
• Google Calendar
• Flickr
• Rojo
• Meebo
• Backpack
• Server-side Component
– Communicates with the database, or web
service
– Can be written in any server-side language
(PHP, ASP, Coldfusion, etc)
• Client-side Component
– Written in Javascript, often uses XMLHttp
– Accesses the server side page in the
background
Hidden Frame Method
• Communication with server takes
place in a frame that user can’t see
• Back and Forward buttons still work
• If something goes wrong user
receives no notification
XMLHttp Method
• Code is cleaner and easier to read
• Able to determine if there is a failure
• No browser history, Back and
Forward buttons break
• Error checking in Forms
• AutoSuggest
• Drag and Drop objects functionality
• Dynamically move view around on image
or map
• Preload content you want to show later
• Apply limits to search results and get new
results quickly
AJAX for Libraries
• Browsing subject headings
• “Pre-displaying” indexes and
databases categories
• Complex ILL or contact forms
• Federated Search
• OPAC and digital library interfaces
• Phoenix Live OPAC - OCLC Research (
http://phoenix.orhost.org)
• Federated Search - Curtin University of
Technology Library (Perth, Western
Australia) (
http://library.curtin.edu.au/cgi-bin/search/search.cgi?query=&
)
• Guesstimate - Virginia Tech Libraries (
http://addison.vt.edu)
AJAX – Sample Applications
• PageInsert - WorldCat Form
• BrowseSearch - LOC Subject
Headings
* PageInsert code available for download at
http://www.lib.montana.edu/~jason/files.php
Code Sample #1: WorldCat
Form
WorldCat XML file to provide content
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<content>
<header>What is Open WorldCat?</header>
<description>The Open WorldCat program makes records of
library-owned materials in OCLC's WorldCat database
available to Web users on popular Internet search,
bibliographic and bookselling sites. Links to content
in library collections—books, serials, digital images
and many other formats—appear alongside links to
traditional Web content.</description>
<sourceDomain>worldcatlibraries.org</sourceDomain>
<sourceUrl>http://worldcatlibraries.org/wcpa/isbn/0471777
781</sourceUrl>
</content>
Code Sample #1: Explanation
• Our source file
• Various and sundry factoids about
WorldCat, some associated urls
• header and description element to
populate the heading and description of
the content
• sourceDomain will give an action value to
our WorldCat search form
• sourceUrl element will provide a link to an
Open Worldcat record
Code Sample #2: WorldCat
Form
Web page for user interface and display
…
<div id="container">
<div id="main"><a name="mainContent"></a>
<h1>Find it in a Library. Use Open WorldCat.</h1>
<p><a onclick="createRequest('xml/worldcat.xml');"
href="#show">+ Learn more about Open Worldcat</a></p>
<div id="content"></div>
</div>
<!-- end main div -->
</div>
<!-- end container div -->
…
Code Sample #2: Explanation
• XHTML form that gives action to our
script
• Notice the javascript “onclick” event
handler on <p> tag
• <div id=“content”> will be populated
with script messages OR new html
tags received via our Ajax requests
Code Sample #3: WorldCat Form
Using the XMLHttpRequest Object
//creates browser specific request using XmlHttpRequest Object
function createRequest(url)
{
if(window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
request = new ActiveXObject("MSXML2.XMLHTTP");
}
else
{
alert("Please upgrade to a newer browser to use the full
functionality of our site.");
}
makeRequest(url);
}
//sends request using GET HTTP method to grab external data
function makeRequest(url)
{
request.onreadystatechange = parseData;
request.open("GET", url, true);
request.send(null);
}
Code Sample #3: Explanation
• First part of our javascript
• Creates the XMLHttpRequest
• Using the if and else statements to
check for Web browsers’ different
implementations of
XMLHttpRequest
• Ends with makeRequest function
Code Sample #4: WorldCat
FormCommunicating the status of our request
//checks state of HTTP request and gives brief status note to user
function communicateStatus(obj)
{
if(obj.readyState == 0) { document.getElementById('content').innerHTML =
"Sending Request..."; }
if(obj.readyState == 1) { document.getElementById('content').innerHTML =
"Loading Response..."; }
if(obj.readyState == 2) { document.getElementById('content').innerHTML =
"Response Loaded..."; }
if(obj.readyState == 3) { document.getElementById('content').innerHTML =
"Response Ready..."; }
if(obj.readyState == 4)
{
if(obj.status == 200)
{
return true;
}
else if(obj.status == 404)
{
// Add a custom message or redirect the user to another page
document.getElementById('content').innerHTML = "File not found";
}
else
{
document.getElementById('content').innerHTML = "There was a
problem retrieving the XML.";
}
}
}
Code Sample #4: Explanation
• Next part of our javascript
• Displays different messages to the user
based on the status of the request on the
server
• uses the “obj” variable which was created
earlier when we called the
XMLHttpRequest
• First peek at Document Object Model
(DOM) in action
Code Sample #5: WorldCat Form
Using the DOM (Document Object Model)
//loads data from external file into page, breaks out variables from sections of file, and
populates html with specific variable values
function parseData()
{
if(communicateStatus(request))
{
//declare format of the data to be parsed and retrieved
var response = request.responseXML.documentElement;
var header = response.getElementsByTagName('header')[0].firstChild.data;
var description = response.getElementsByTagName('description')[0].firstChild.data;
var sourceDomain = response.getElementsByTagName('sourceDomain')
[0].firstChild.data;
var sourceUrl = response.getElementsByTagName('sourceUrl')[0].firstChild.data;
document.getElementById('content').innerHTML = "<h2>" + header + "</h2>n"
+ "<p>" + description + "</p>n"
+ "<form method="get"
action="http://www.google.com/search">n"
+ "<fieldset>n"
+ "<label>Search Open WorldCat:</label>n"
+ "<input type="hidden" name="as_sitesearch"
value='" + sourceDomain + "'>n"
+ "<input type="text" name="q" size="40"
maxlength="255" value="">n"
+ "<input class="submit" type="submit" name="sa"
value="Find Books">n"
+ "</fieldset>n"
+ "</form>n"
+ "<p><a href='" + sourceUrl + "'>View a sample Open
WorldCat record</a></p>n";
}
}
Code Sample #5: Explanation
• Last part of our javascript
• Applies DOM to give us a standard
means of modeling the structure of
XHTML or XML documents
• DOM functions like
“getElementsByTagName”
• Grab data and push it into prescribed
sections of our XHTML page
Code Sample #6: WorldCat Form
CSS (Cascading Style Sheets)
…
/* =container
----------------------------------------------- */
div#container {width:65em;margin:0 auto;background:#fff;}
/* =main
----------------------------------------------- */
div#main {width:63em;margin:0 auto;padding:1em .5em 2em .5em;}
/* =content
----------------------------------------------- */
div#content {width:95%;margin:0 auto;}
#content p.warn {color:red;}
/* =forms
----------------------------------------------- */
form {padding:10px;border-top:1px solid #ccc;border-right:2px solid
#ccc;border-bottom:2px solid #ccc;border-left:1px solid #ccc;background-
color:#F2F2F2;}
fieldset {border:none;}
label {font-size:1.2em;color:#2b4268;vertical-align:middle;cursor:pointer;}
input, select, textarea {width:25em;font:1.0em verdana,arial,sans-
serif;padding:3px;margin:3px;border:1px solid gray;border-color:#AAA #DDD
#DDD #AAA;vertical-align:middle;}
input:focus {border:1px #000 solid;}
input.submit {width:10em;font-size:.90em;color:#2b4268;}
…
Code Sample #6: Explanation
• Part of our CSS file
• Means of passing style rules for
different pieces of the Web page
• <div> tags are given specific,
relative widths, <form> tags are
styled with attractive borders
Final thoughts – What’s Next?
• That’s AJAX and an AJAX
application in a nutshell.
• Consider AJAX advantages and
disadvantages
• Fundamentals of method are there
• Keep practicing and learning
AJAX - Further References
• Articles
– Ajax: A New Approach to Web Applications by Jesse James Garrett
http://www.adaptivepath.com/publications/essays/archives/000385.php
– Ajax gives software a fresh look (from CNET News)
http://beta.news.com.com/Ajax+gives+software+a+fresh+look/2100-1007_3-
5886709.html?
– Weighing the Alternatives (from ajax info)
http://www.ajaxinfo.com/default~viewart~8.htm
• Resources
– XMLHttpRequest & Ajax Based Applications (from Fiftyfoureleven.com)
http://www.fiftyfoureleven.com/resources/programming/xmlhttprequest/
– Foundations of Ajax by Ryan Asleson, Nathaniel T. Schutta
ISBN: 1590595823 http://www.worldcatlibraries.org/wcpa/isbn/1590595823
• Tutorials
– Getting Started with AJAX (from A List Apart)
http://www.alistapart.com/articles/gettingstartedwithajax
– AJAX:Getting Started (from Mozilla Developer Center)
http://developer.mozilla.org/en/docs/AJAX:Getting_Started
– Dynamic HTML and XML: The XMLHTTPRequest Object (from Apple
Developer Connection)
http://developer.apple.com/internet/webcontent/xmlhttpreq.html
– Mastering Ajax, Part 1: Introduction to Ajax (from IBM developerWorks)
http://www-128.ibm.com/developerworks/web/library/wa-ajaxintro1.html?
ca=dgr-wikiAJAXinto1
Contact Information
Karen Coombs
University of Houston Libraries
Web Services Librarian
kacoombs@uh.edu
http://librarywebchic.net/
713-743-3713
Jason A. Clark
Montana State University Libraries
Digital Initiatives Librarian
jaclark@montana.edu
www.jasonclark.info
406-994-6801
Code Sample #1: LOC Subject
Headings
Web page for user interface and display
<div id="main"><a name="mainContent"></a>
<h2 class="mainHeading">CIL 2006 :: Example: Library of
Congress BrowseSearch</h2>
<form id="searchbox" action="browseSearch.php"
method="post">
<p><label
for="query"><strong>BrowseSearch:</strong></label>&nbs
p;<input type="text" name="query" autocomplete="off"
id="query" onKeyUp="preSearch()" />
&nbsp;<img id="searchStatus" alt="searching..."
src="./meta/img/spinner.gif" /></p>
</form>
<div id="result">&nbsp;</div>
</div>
Code Sample #1: Explanation
• XHTML form that gives action to our
script
• Note the javascript “onKeyUp” event
handler on <input> tag
• <input> also given “name” and “id”
• <div id=“result”> will be populated
with script messages OR new html
tags received via our Ajax requests
Code Sample #2: LOC Subject
HeadingsUsing javascript to “presearch” database
function preSearch() {
//Put the form data into a variable
var theQuery = document.getElementById('query').value;
//If the form data is *not* blank, query the DB and return the
results
if(theQuery !== "") {
//If search pauses when fetching, change the content of the
"result" DIV to "Searching..."
document.getElementById('result').innerHTML = "Searching...";
//This sets a variable with the URL (and query strings) to our
PHP script
var url = 'browseSearch.php?q=' + theQuery;
//Open the URL above "asynchronously" (that's what the "true"
is for) using the GET method
xmlhttp.open('GET', url, true);
//Check that the PHP script has finished sending us the result
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
//Replace the content of the "result" DIV with the
result returned by the PHP script
document.getElementById('result').innerHTML = xmlhttp.responseText
+ ' ';
Code Sample #2: Explanation
• Piece of javascript that creates
instant search
• Talks to server-side PHP script -
browseSearch.php
• Uses DOM to populate <div
id=“result”> with search results
PHP search loc database script
<?php
//declare variables to be used in query and display
$keywords = $_GET['query'];
$link = "<p><a href="browseSearch.php">Library of Congress LiveSearch</a></p>";
...
// bring database parameters and functions onto page
...
//form sql statement
$query = "SELECT subject_id, label, callno FROM subject WHERE label LIKE '%
$keywords%' ORDER BY callno ASC";
//store sql result as an array
$result = mysql_query($query) or die('<p class="warn">Error retrieving subjects
from loc database!<br />'.
'Error: ' . mysql_error() . '</p>');
//create message if no rows match search terms
…
//format sql result for display
while($record = mysql_fetch_object($result))
{
echo '<dl><dt><strong>'.stripslashes($record->label).'</strong></dt>';
echo '<dd>Call Number Range: '.stripslashes($record->callno).'</dd>';
echo '<dd><a
href="http://www.lib.montana.edu/help/locationguide.html">Find Call Number on
Library Floor Map</a></dd></dl>';
echo '<hr size="1" />';
}
echo $link;
?>
Code Sample #3: Explanation
• Piece of PHP script that searches
loc database
• Basic SQL SELECT statement
• Uses <dl> to format search results

Mais conteúdo relacionado

Mais procurados

Using Ajax In Domino Web Applications
Using Ajax In Domino Web ApplicationsUsing Ajax In Domino Web Applications
Using Ajax In Domino Web Applications
dominion
 
An Introduction to Ajax Programming
An Introduction to Ajax ProgrammingAn Introduction to Ajax Programming
An Introduction to Ajax Programming
hchen1
 
Ajax Introduction Presentation
Ajax   Introduction   PresentationAjax   Introduction   Presentation
Ajax Introduction Presentation
thinkphp
 

Mais procurados (20)

Overview of AJAX
Overview of AJAXOverview of AJAX
Overview of AJAX
 
Architecture in Ajax Applications
Architecture in Ajax ApplicationsArchitecture in Ajax Applications
Architecture in Ajax Applications
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Ajax presentation
Ajax presentationAjax presentation
Ajax presentation
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Ajax
AjaxAjax
Ajax
 
Using Ajax In Domino Web Applications
Using Ajax In Domino Web ApplicationsUsing Ajax In Domino Web Applications
Using Ajax In Domino Web Applications
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
Ajax Presentation
Ajax PresentationAjax Presentation
Ajax Presentation
 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Ajax:From Desktop Applications towards Ajax Web Applications
Ajax:From Desktop Applications towards Ajax Web ApplicationsAjax:From Desktop Applications towards Ajax Web Applications
Ajax:From Desktop Applications towards Ajax Web Applications
 
Ajax and PHP
Ajax and PHPAjax and PHP
Ajax and PHP
 
Ajax
AjaxAjax
Ajax
 
What is Ajax technology?
What is Ajax technology?What is Ajax technology?
What is Ajax technology?
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
An Introduction to Ajax Programming
An Introduction to Ajax ProgrammingAn Introduction to Ajax Programming
An Introduction to Ajax Programming
 
Ajax presentation
Ajax presentationAjax presentation
Ajax presentation
 
Ajax Introduction Presentation
Ajax   Introduction   PresentationAjax   Introduction   Presentation
Ajax Introduction Presentation
 
Ajax
AjaxAjax
Ajax
 

Semelhante a Ajax workshop

Ajax for Libraries
Ajax for LibrariesAjax for Libraries
Ajax for Libraries
guest5aa3e8
 
SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...
Liam Cleary [MVP]
 

Semelhante a Ajax workshop (20)

Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 
Ajax for Libraries
Ajax for LibrariesAjax for Libraries
Ajax for Libraries
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
 
AJAX
AJAXAJAX
AJAX
 
ITI006En-AJAX
ITI006En-AJAXITI006En-AJAX
ITI006En-AJAX
 
Ajax
AjaxAjax
Ajax
 
SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...
 
Ajax Lecture Notes
Ajax Lecture NotesAjax Lecture Notes
Ajax Lecture Notes
 
Ajax
AjaxAjax
Ajax
 
Introduction to AJAX
Introduction to AJAXIntroduction to AJAX
Introduction to AJAX
 
Ajax Tuturial
Ajax TuturialAjax Tuturial
Ajax Tuturial
 
Ajax
AjaxAjax
Ajax
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
 
Jqueryppt (1)
Jqueryppt (1)Jqueryppt (1)
Jqueryppt (1)
 

Mais de WBUTTUTORIALS (12)

Software testing-and-analysis
Software testing-and-analysisSoftware testing-and-analysis
Software testing-and-analysis
 
Query optimisation
Query optimisationQuery optimisation
Query optimisation
 
Fuzzy logic-introduction
Fuzzy logic-introductionFuzzy logic-introduction
Fuzzy logic-introduction
 
Failure mode-and-effects-analysis
Failure mode-and-effects-analysisFailure mode-and-effects-analysis
Failure mode-and-effects-analysis
 
Direct memory access
Direct memory accessDirect memory access
Direct memory access
 
Cost volume-profit-relationships
Cost volume-profit-relationshipsCost volume-profit-relationships
Cost volume-profit-relationships
 
Control unit-implementation
Control unit-implementationControl unit-implementation
Control unit-implementation
 
Relational model
Relational modelRelational model
Relational model
 
Query processing-and-optimization
Query processing-and-optimizationQuery processing-and-optimization
Query processing-and-optimization
 
Data communications-concepts
Data communications-conceptsData communications-concepts
Data communications-concepts
 
Ajax toolkit-framework
Ajax toolkit-frameworkAjax toolkit-framework
Ajax toolkit-framework
 
Ajax
AjaxAjax
Ajax
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Último (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Ajax workshop

  • 1. AJAX Workshop Karen A. Coombs University of Houston Libraries Jason A. Clark Montana State University Libraries
  • 2. Outline 1. What you’re in for… 2. What’s AJAX? 3. Why AJAX? 4. Look at some AJAX examples 5. AJAX for Libraries 6. Walkthrough sample AJAX application
  • 3. What you’re in for… • A discussion about an emerging web application framework • An introduction to the essential elements of AJAX • How does AJAX fit into libraries? • Walkthrough the code of a working AJAX application
  • 4. What is AJAX? • Asynchronous Javascript and XML – Not all AJAX apps involve XML • Combination of technologies – XHTML, CSS, DOM – XML, XSLT, XMLHttp, JavaScript – Some server scripting language • A method for building more responsive and interactive applications
  • 5. AJAX Components XHTML and CSS Ajax applies these familiar Web standards for styling the look and feel of a page and to markup those areas on a page that will be targeted for data updates. DOM (document object model) Ajax uses the DOM to manipulate dynamic page views for data and to walkthrough documents to “cherrypick” data. The DOM enables certain pieces of an Ajax page to be transformed and updated with data. XML, JSON (Javascript Object Notation), HTML, or plain text Ajax can use any of these standards to provide structure to the data it passes to and from a page. XMLHttpRequest object The heavy lifter for Ajax: It’s a javascript object embedded in most modern browsers that sets up data request/response pipelines between client and server. Javascript Lightweight programming language that Ajax uses for instructions to bind all of the components together.
  • 6. Why AJAX? • Want to make your applications more interactive • Want to incorporate data from external Web Services • Don’t want your users to have to download a plugin
  • 7. • Client scripting – Web browser does all the work • Server Scripting – Web server does all the work • AJAX leverages both client and server side scripting
  • 9. AJAX Web Interaction • What you don’t see • Data reload happens in the background • JavaScript queries the server to get the proper data without you knowing it • Page updates without a screen “reload”
  • 10. Potential Problems • Javascript MUST be enabled • Back button doesn’t always work • Pages can be difficult to bookmark • Search engines may not be able to index all portions of an AJAX site • Cross browser differences in how XML is dealt with
  • 11. Some AJAX examples • Google Calendar • Flickr • Rojo • Meebo • Backpack
  • 12. • Server-side Component – Communicates with the database, or web service – Can be written in any server-side language (PHP, ASP, Coldfusion, etc) • Client-side Component – Written in Javascript, often uses XMLHttp – Accesses the server side page in the background
  • 13. Hidden Frame Method • Communication with server takes place in a frame that user can’t see • Back and Forward buttons still work • If something goes wrong user receives no notification
  • 14. XMLHttp Method • Code is cleaner and easier to read • Able to determine if there is a failure • No browser history, Back and Forward buttons break
  • 15. • Error checking in Forms • AutoSuggest • Drag and Drop objects functionality • Dynamically move view around on image or map • Preload content you want to show later • Apply limits to search results and get new results quickly
  • 16. AJAX for Libraries • Browsing subject headings • “Pre-displaying” indexes and databases categories • Complex ILL or contact forms • Federated Search • OPAC and digital library interfaces
  • 17. • Phoenix Live OPAC - OCLC Research ( http://phoenix.orhost.org) • Federated Search - Curtin University of Technology Library (Perth, Western Australia) ( http://library.curtin.edu.au/cgi-bin/search/search.cgi?query=& ) • Guesstimate - Virginia Tech Libraries ( http://addison.vt.edu)
  • 18. AJAX – Sample Applications • PageInsert - WorldCat Form • BrowseSearch - LOC Subject Headings * PageInsert code available for download at http://www.lib.montana.edu/~jason/files.php
  • 19. Code Sample #1: WorldCat Form WorldCat XML file to provide content <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <content> <header>What is Open WorldCat?</header> <description>The Open WorldCat program makes records of library-owned materials in OCLC's WorldCat database available to Web users on popular Internet search, bibliographic and bookselling sites. Links to content in library collections—books, serials, digital images and many other formats—appear alongside links to traditional Web content.</description> <sourceDomain>worldcatlibraries.org</sourceDomain> <sourceUrl>http://worldcatlibraries.org/wcpa/isbn/0471777 781</sourceUrl> </content>
  • 20. Code Sample #1: Explanation • Our source file • Various and sundry factoids about WorldCat, some associated urls • header and description element to populate the heading and description of the content • sourceDomain will give an action value to our WorldCat search form • sourceUrl element will provide a link to an Open Worldcat record
  • 21. Code Sample #2: WorldCat Form Web page for user interface and display … <div id="container"> <div id="main"><a name="mainContent"></a> <h1>Find it in a Library. Use Open WorldCat.</h1> <p><a onclick="createRequest('xml/worldcat.xml');" href="#show">+ Learn more about Open Worldcat</a></p> <div id="content"></div> </div> <!-- end main div --> </div> <!-- end container div --> …
  • 22. Code Sample #2: Explanation • XHTML form that gives action to our script • Notice the javascript “onclick” event handler on <p> tag • <div id=“content”> will be populated with script messages OR new html tags received via our Ajax requests
  • 23. Code Sample #3: WorldCat Form Using the XMLHttpRequest Object //creates browser specific request using XmlHttpRequest Object function createRequest(url) { if(window.XMLHttpRequest) { request = new XMLHttpRequest(); } else if(window.ActiveXObject) { request = new ActiveXObject("MSXML2.XMLHTTP"); } else { alert("Please upgrade to a newer browser to use the full functionality of our site."); } makeRequest(url); } //sends request using GET HTTP method to grab external data function makeRequest(url) { request.onreadystatechange = parseData; request.open("GET", url, true); request.send(null); }
  • 24. Code Sample #3: Explanation • First part of our javascript • Creates the XMLHttpRequest • Using the if and else statements to check for Web browsers’ different implementations of XMLHttpRequest • Ends with makeRequest function
  • 25. Code Sample #4: WorldCat FormCommunicating the status of our request //checks state of HTTP request and gives brief status note to user function communicateStatus(obj) { if(obj.readyState == 0) { document.getElementById('content').innerHTML = "Sending Request..."; } if(obj.readyState == 1) { document.getElementById('content').innerHTML = "Loading Response..."; } if(obj.readyState == 2) { document.getElementById('content').innerHTML = "Response Loaded..."; } if(obj.readyState == 3) { document.getElementById('content').innerHTML = "Response Ready..."; } if(obj.readyState == 4) { if(obj.status == 200) { return true; } else if(obj.status == 404) { // Add a custom message or redirect the user to another page document.getElementById('content').innerHTML = "File not found"; } else { document.getElementById('content').innerHTML = "There was a problem retrieving the XML."; } } }
  • 26. Code Sample #4: Explanation • Next part of our javascript • Displays different messages to the user based on the status of the request on the server • uses the “obj” variable which was created earlier when we called the XMLHttpRequest • First peek at Document Object Model (DOM) in action
  • 27. Code Sample #5: WorldCat Form Using the DOM (Document Object Model) //loads data from external file into page, breaks out variables from sections of file, and populates html with specific variable values function parseData() { if(communicateStatus(request)) { //declare format of the data to be parsed and retrieved var response = request.responseXML.documentElement; var header = response.getElementsByTagName('header')[0].firstChild.data; var description = response.getElementsByTagName('description')[0].firstChild.data; var sourceDomain = response.getElementsByTagName('sourceDomain') [0].firstChild.data; var sourceUrl = response.getElementsByTagName('sourceUrl')[0].firstChild.data; document.getElementById('content').innerHTML = "<h2>" + header + "</h2>n" + "<p>" + description + "</p>n" + "<form method="get" action="http://www.google.com/search">n" + "<fieldset>n" + "<label>Search Open WorldCat:</label>n" + "<input type="hidden" name="as_sitesearch" value='" + sourceDomain + "'>n" + "<input type="text" name="q" size="40" maxlength="255" value="">n" + "<input class="submit" type="submit" name="sa" value="Find Books">n" + "</fieldset>n" + "</form>n" + "<p><a href='" + sourceUrl + "'>View a sample Open WorldCat record</a></p>n"; } }
  • 28. Code Sample #5: Explanation • Last part of our javascript • Applies DOM to give us a standard means of modeling the structure of XHTML or XML documents • DOM functions like “getElementsByTagName” • Grab data and push it into prescribed sections of our XHTML page
  • 29. Code Sample #6: WorldCat Form CSS (Cascading Style Sheets) … /* =container ----------------------------------------------- */ div#container {width:65em;margin:0 auto;background:#fff;} /* =main ----------------------------------------------- */ div#main {width:63em;margin:0 auto;padding:1em .5em 2em .5em;} /* =content ----------------------------------------------- */ div#content {width:95%;margin:0 auto;} #content p.warn {color:red;} /* =forms ----------------------------------------------- */ form {padding:10px;border-top:1px solid #ccc;border-right:2px solid #ccc;border-bottom:2px solid #ccc;border-left:1px solid #ccc;background- color:#F2F2F2;} fieldset {border:none;} label {font-size:1.2em;color:#2b4268;vertical-align:middle;cursor:pointer;} input, select, textarea {width:25em;font:1.0em verdana,arial,sans- serif;padding:3px;margin:3px;border:1px solid gray;border-color:#AAA #DDD #DDD #AAA;vertical-align:middle;} input:focus {border:1px #000 solid;} input.submit {width:10em;font-size:.90em;color:#2b4268;} …
  • 30. Code Sample #6: Explanation • Part of our CSS file • Means of passing style rules for different pieces of the Web page • <div> tags are given specific, relative widths, <form> tags are styled with attractive borders
  • 31. Final thoughts – What’s Next? • That’s AJAX and an AJAX application in a nutshell. • Consider AJAX advantages and disadvantages • Fundamentals of method are there • Keep practicing and learning
  • 32. AJAX - Further References • Articles – Ajax: A New Approach to Web Applications by Jesse James Garrett http://www.adaptivepath.com/publications/essays/archives/000385.php – Ajax gives software a fresh look (from CNET News) http://beta.news.com.com/Ajax+gives+software+a+fresh+look/2100-1007_3- 5886709.html? – Weighing the Alternatives (from ajax info) http://www.ajaxinfo.com/default~viewart~8.htm • Resources – XMLHttpRequest & Ajax Based Applications (from Fiftyfoureleven.com) http://www.fiftyfoureleven.com/resources/programming/xmlhttprequest/ – Foundations of Ajax by Ryan Asleson, Nathaniel T. Schutta ISBN: 1590595823 http://www.worldcatlibraries.org/wcpa/isbn/1590595823 • Tutorials – Getting Started with AJAX (from A List Apart) http://www.alistapart.com/articles/gettingstartedwithajax – AJAX:Getting Started (from Mozilla Developer Center) http://developer.mozilla.org/en/docs/AJAX:Getting_Started – Dynamic HTML and XML: The XMLHTTPRequest Object (from Apple Developer Connection) http://developer.apple.com/internet/webcontent/xmlhttpreq.html – Mastering Ajax, Part 1: Introduction to Ajax (from IBM developerWorks) http://www-128.ibm.com/developerworks/web/library/wa-ajaxintro1.html? ca=dgr-wikiAJAXinto1
  • 33. Contact Information Karen Coombs University of Houston Libraries Web Services Librarian kacoombs@uh.edu http://librarywebchic.net/ 713-743-3713 Jason A. Clark Montana State University Libraries Digital Initiatives Librarian jaclark@montana.edu www.jasonclark.info 406-994-6801
  • 34. Code Sample #1: LOC Subject Headings Web page for user interface and display <div id="main"><a name="mainContent"></a> <h2 class="mainHeading">CIL 2006 :: Example: Library of Congress BrowseSearch</h2> <form id="searchbox" action="browseSearch.php" method="post"> <p><label for="query"><strong>BrowseSearch:</strong></label>&nbs p;<input type="text" name="query" autocomplete="off" id="query" onKeyUp="preSearch()" /> &nbsp;<img id="searchStatus" alt="searching..." src="./meta/img/spinner.gif" /></p> </form> <div id="result">&nbsp;</div> </div>
  • 35. Code Sample #1: Explanation • XHTML form that gives action to our script • Note the javascript “onKeyUp” event handler on <input> tag • <input> also given “name” and “id” • <div id=“result”> will be populated with script messages OR new html tags received via our Ajax requests
  • 36. Code Sample #2: LOC Subject HeadingsUsing javascript to “presearch” database function preSearch() { //Put the form data into a variable var theQuery = document.getElementById('query').value; //If the form data is *not* blank, query the DB and return the results if(theQuery !== "") { //If search pauses when fetching, change the content of the "result" DIV to "Searching..." document.getElementById('result').innerHTML = "Searching..."; //This sets a variable with the URL (and query strings) to our PHP script var url = 'browseSearch.php?q=' + theQuery; //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method xmlhttp.open('GET', url, true); //Check that the PHP script has finished sending us the result xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { //Replace the content of the "result" DIV with the result returned by the PHP script document.getElementById('result').innerHTML = xmlhttp.responseText + ' ';
  • 37. Code Sample #2: Explanation • Piece of javascript that creates instant search • Talks to server-side PHP script - browseSearch.php • Uses DOM to populate <div id=“result”> with search results
  • 38. PHP search loc database script <?php //declare variables to be used in query and display $keywords = $_GET['query']; $link = "<p><a href="browseSearch.php">Library of Congress LiveSearch</a></p>"; ... // bring database parameters and functions onto page ... //form sql statement $query = "SELECT subject_id, label, callno FROM subject WHERE label LIKE '% $keywords%' ORDER BY callno ASC"; //store sql result as an array $result = mysql_query($query) or die('<p class="warn">Error retrieving subjects from loc database!<br />'. 'Error: ' . mysql_error() . '</p>'); //create message if no rows match search terms … //format sql result for display while($record = mysql_fetch_object($result)) { echo '<dl><dt><strong>'.stripslashes($record->label).'</strong></dt>'; echo '<dd>Call Number Range: '.stripslashes($record->callno).'</dd>'; echo '<dd><a href="http://www.lib.montana.edu/help/locationguide.html">Find Call Number on Library Floor Map</a></dd></dl>'; echo '<hr size="1" />'; } echo $link; ?>
  • 39. Code Sample #3: Explanation • Piece of PHP script that searches loc database • Basic SQL SELECT statement • Uses <dl> to format search results