onclick="return executeCartAction(this);">Save for lateronclick="return executeCartAction(this);">RemoveIn both cases, a JavaScript function is executed as a result of the click event. If that
function returns false, the link isn??™t followed anymore. If the function returns true or it??™s not
executed at all, the link is followed, and the shopping cart gets modified using in a non-AJAX
fashion.
The Add to Cart buttons are handled similarly, except in this case the JavaScript function
is called when submitting the form:
onsubmit="return addProductToCart(this);">
Figure 13-2 describes what happens when a visitor clicks an Add to Cart button. The
process is similar to that of updating shopping cart items, which we??™ll also implement in this
chapter. As you can see, no matter whether the visitor has JavaScript or not, the same end
result is achieved. The difference lies in the responsiveness; in the AJAX scenario the user
doesn??™t have to wait a few seconds for the page to reload.
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 409
Figure 13-2. Degradable AJAX feature
JavaScript code in the
web page calls the
server page asynchronously
to inform it of the user action
User has
JavaScript
enabled
AJAX Scenario
Traditional Scenario
When the server??™s response
is received, the user page
is updated to reflect the
shopping cart changes The user
receives a
new page
that reflects
that the
product has
been added
to the cart Form is submitted to server
Yes
No
User clicks
Add to Cart
button
??? ??? ??? ???
)
Default Joe Happy Joe
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 410
Is AJAX Always Suitable?
We end the theoretical part of the chapter with a warning: AJAX can improve your visitors??™ experience
with your web site, but it can also worsen it when used inappropriately.
Pages:
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526