length && !xmlHttp; i++)
{
try
{
// Try to create XMLHttpRequest object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) {} // Ignore potential error
}
}
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 415
// If the XMLHttpRequest object was created successfully, return it
if (xmlHttp)
{
return xmlHttp;
}
// If an error happened, pass it to handleError
else
{
handleError("Error creating the XMLHttpRequest object.");
}
}
// Displays an the error message or degrades to non-AJAX behavior
function handleError($message)
{
// Ignore errors if showErrors is false
if (showErrors)
{
// Display error message
alert("Error encountered: \n" + $message);
return false;
}
// Fall back to non-AJAX behavior
else if (!actionObject.tagName)
{
return true;
}
// Fall back to non-AJAX behavior by following the link
else if (actionObject.tagName == 'A')
{
window.location = actionObject.href;
}
// Fall back to non-AJAX behavior by submitting the form
else if (actionObject.tagName == 'FORM')
{
actionObject.submit();
}
}
// Adds a product to the shopping cart
function addProductToCart(form)
{
// Display "Updating" message
document.getElementById('updating').style.visibility = 'visible';
// Degrade to classical form submit if XMLHttpRequest is not available
if (!xmlHttp) return true;
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 416
// Create the URL we open asynchronously
request = form.
Pages:
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532