Prototype and onComplete

My first big project at my new job has been to rewrite the forward-facing part of our booking engine. The old design used very static page structures with a lot of form submits, and was not navigation-friendly. Moving through the process required a lot of submit and back actions, with the associated page loads that go along with that - very time consuming and frustrating.
The calendar portion of the page was an ugly behemoth in and of itself, consisting of an HTML table element (with borders!) embedded in an iframe. Woof. I definitely wanted to get away from the page refreshes, as well as provide a much easier method of selecting unit types, dates, and length of stay. Enter AJAX and Prototype.

AJAX stands for Asynchronous Javascript And XML, and Prototype is the javascript library I am using for this functionality. With AJAX, calls to the database happen behind the scenes and update content on the page without having to refresh the page. However, I ran into a minor difficulty while working on this project, namely, that certain functions have to be executed upon completion of the AJAX call.

Prototype provides a way to specify what happens after the AJAX call has finished, and that method is onComplete.

[js]var params = 'function=getAvailsByWeekNumber&invID=101';
try { var myAjax = new Ajax.Request(strURL, {method:'get', parameters: params, asynchronous: true, onComplete: function(request) {
// Call some function
someFunction(request.responseText);
},
onException: function(req,exception) {
alert("The request had a fatal exception thrown.nn" + exception);
},
onFailure: function(request) { return reloadPage(); }
});
}
catch(Exception) {alert("Exception detected: " + Exception); }[/js]

In this example, I am calling someFunction upon completion of the AJAX request, and I am passing the result of the AJAX call to the function. But what if I needed to pass something else to the function, like the inventory ID defined in the query string?

As it turns out, IE and Firefox behave differently when it comes to doing this (go figure...). In Firefox, the following works:

[js]onComplete: function(request)

{

// Get the parameter property of the Ajax.Request object

var params = this.parameters;

// Parse the key/value pairs and pass to function

}[/js]

This fails in IE, because this.parameters is undefined. However, the params variable defined from above the try/catch block is available, where in Firefox it is not.

Hopefully, this helps someone out...

[tags]Prototype, AJAX, Web+2.0, javascript, XML, asynchronous, Firefox, IE, Internet+Explorer, onComplete, ResortScope, Ajax.Request, code, programming, Beyrent[/tags]

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011