Head First Ajax(1).pdf

(50698 KB) Pobierz
280521538 UNPDF
280521538.011.png
Chapter 1. using ajax
Table of Contents
Chapter 1. using ajax. ................................................................................... 1
Section 1.1. Web pages: the old-fashioned approach ..................................................................................................................... 2
Section 1.2. Web pages reinvented ................................................................................................................................................. 3
Section 1.3. So what makes a page "Ajax"?. .................................................................................................................................... 5
Section 1.4. Rob's Rock 'n' Roll Memorabilia ................................................................................................................................. 6
Section 1.5. Ajax and rock 'n' roll in 5 steps .................................................................................................................................. 12
Section 1.6. Step 1: Modify the XHTML ........................................................................................................................................ 14
Section 1.7. Step 2: Initialize the JavaScript ................................................................................................................................. 16
Section 1.8. Step 3: Create a request object. ................................................................................................................................. 20
Section 1.9. Step 4: Get the item's details ..................................................................................................................................... 22
Section 1.10. Let's write the code for requesting an item's details ............................................................................................... 24
Section 1.11. Always make sure you have a request object before working with it ...................................................................... 25
Section 1.12. The request object is just an object ......................................................................................................................... 26
Section 1.13. Hey, server... will you call me back at displayDetails(), please? ............................................................................. 27
Section 1.14. Use send() to send your request .............................................................................................................................. 28
Section 1.15. The server usually returns data to Ajax requests .................................................................................................... 30
Section 1.16. Ajax is server-agnostic .............................................................................................................................................. 31
Section 1.17. Use a callback function to work with data the server returns ................................................................................. 35
Section 1.18. Get the server's response from the request object's responseText property .......................................................... 36
Section 1.19. Goodbye traditional web apps... .............................................................................................................................. 38
Section 1.20. AjaxAcrostic ............................................................................................................................................................. 39
Chapter 1. using ajax
Head First Ajax By Rebecca M. Riordan ISBN: 9780596515782 Publisher: O'Reilly Prepared for Ann Cherkis, Safari ID: maottw@gmail.com
Print Publication Date: 2008/08/26 User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
280521538.012.png
 
Chapter 1. using ajax Page 1
1
using ajax
Web Apps for a New
Generation
,·OOMXVWWDNHDOLWWOHQDS
ZKLOH,·PZDLWLQJIRUP\
ZHEDSSWRUHVSRQG
Tired of waiting around for your page to reload?
)UXVWUDWHGE\FOXQN\ZHEDSSOLFDWLRQLQWHUIDFHV",W¶VWLPHWRJLYH\RXUZHEDSSVWKDW
VOLFNUHVSRQVLYHGHVNWRSIHHO$QGKRZGR\RXGRWKDW":LWK
Ajax
\RXUWLFNHWWR
EXLOGLQJ,QWHUQHWDSSOLFDWLRQVWKDWDUH
more interactive, more responsive,
DQG
easier
6RVNLS\RXUQDSLW¶VWLPHWRSXWVRPHSROLVKRQ\RXUZHEDSSV,W¶VWLPHWR
JHWULGRIXQQHFHVVDU\DQGVORZIXOOSDJHUHIUHVKHVIRUHYHU
this is a new chapter
1
Chapter 1. using ajax
Head First Ajax By Rebecca M. Riordan ISBN: 9780596515782 Publisher: O'Reilly Prepared for Ann Cherkis, Safari ID: maottw@gmail.com
Print Publication Date: 2008/08/26 User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
to use
280521538.013.png 280521538.001.png
 
Chapter 1. using ajax Page 2
old-fashioned web apps
Web pages: the old-fashioned approach
With traditional web pages and applications, every time a user clicks on
something, the browser sends a request to the server, and the server responds
with a whole new page . Even if your user’s web browser is smart about
caching things like images and cascading style sheets, that’s a lot of traffic
going back and forth between their browser and your server... and a lot of
time that the user sits around waiting for full page refreshes.
chan ging... b ut there ’s still a complete page re fresh.
A nd th e serve r
sends back anothe r
who le page ...
2 Chapter 1
Chapter 1. using ajax
Head First Ajax By Rebecca M. Riordan ISBN: 9780596515782 Publisher: O'Reilly Prepared for Ann Cherkis, Safari ID: maottw@gmail.com
Print Publication Date: 2008/08/26 User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
Most of the t ime, only a single line or i mage is
280521538.002.png 280521538.003.png 280521538.004.png 280521538.005.png
 
Chapter 1. using ajax Page 3
using ajax
Web pages reinvented
Using Ajax, your pages and applications only ask the server for what they
really need—just the parts of a page that need to change, and just the parts
that the server has to provide. That means less traffic, smaller updates, and
less time sitting around waiting for page refreshes.
With Ajax, the browser only sends and receives
the parts of a page that need to change.
a spe cial req uest o bject t hat t he
bro wser se nds to the s erver.
request
function
getDetails {
...
}
request
...an d your cod e tells the
b rowser to update only the
parts of the page t hat
have c hanged.
Sometimes the browser doesn’t have to
talk to the server at all.
Th e script can up date th e
image w ithout t he serve r-side
progra m at al l!
function
getDetails {
...
}
Th e scrip t tells the bro wser
how to update the pa ge... all
witho ut a pa ge refr esh.
you are here
3
Chapter 1. using ajax
Head First Ajax By Rebecca M. Riordan ISBN: 9780596515782 Publisher: O'Reilly Prepared for Ann Cherkis, Safari ID: maottw@gmail.com
Print Publication Date: 2008/08/26 User number: 1673621 Copyright 2008, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
T his ti me, you r page ’s code creat es
280521538.006.png 280521538.007.png 280521538.008.png 280521538.009.png 280521538.010.png
 
Zgłoś jeśli naruszono regulamin