Showing posts with label AJax. Show all posts
Showing posts with label AJax. Show all posts

Tuesday, January 01, 2008

Prototype v1.6 Ajax.Updater and textarea in FireFox v2.x

There is a slight issue with how Prototype v1.6 (and prior) deals with textarea form elements when used in combination with Ajax.Updater. This appears to only be an issue with FireFox v2.0.0.x and not IE7 or Safari v3 for Windows. I have not tested to confirm if this is an issue on non-windows versions of FireFox or other browsers or platforms.

The problem enters when Updater is successful in putting the results content in to the textarea innerHTML, but the contents are not displayed. Most of the solutions that I have been able to find on the web suggests to use a onSuccess: callback then within there, pull the responseText from the response object and shove it in to the value of the textarea.

Example of a BAD use of Ajax.Updater with the textarea:

<form id="formid">
<textarea id="textareaid" />
</form>
Ajax Status: <div id="ajaxStatus"></div>
...
new Ajax.Updater( 'textareaid', 'anSOAapp.jsp', {
parameters: { id: anId, afield : avar },
onFailure: function(){
$('ajaxStatus').update('An error occurred.');
},
onSuccess: function(response) {
$('textareaid').value = response.responseText;
$('ajaxStatus').update( response.status );
}});

How wasteful! Not only in redundant processing (the contents of the textarea is being updated twice!), but also in memory that JavaScript is now having to allocate and use. What is the point of using the Ajax.Updater function if you are just going to overwrite its output?! For small tidbits of data, it may not be noticeable, but for larger data sets, then you can not only risk the out of memory exceptions, but also a non-functional page.

The whole point of utilizing the Ajax.Updater function is to help streamline the whole data flow and to offset the needs to keeping the data in a variable or as a String in JavaScript, which is what occurs when using the responseText field.

There is an easy solution that helps to avoid these problems of redundancy and excessive memory utilization. It is actually quite simple.

Example of the correct use of Ajax.Updater with the textarea:

<form id="formid">
<textarea id="textareaid" />
</form>
Ajax Status: <div id="ajaxStatus"></div>
...
new Ajax.Updater( 'textareaid', 'anSOAapp.jsp', {
parameters: { id: anId, afield : avar },
onFailure: function(){
$('ajaxStatus').update('An error occurred.');
},
onSuccess: function(response) {
if ( Prototype.Browser.Gecko ) {
$('formid').reset();
}
$('ajaxStatus').update( response.status );
}});


Notice that the key element was
$('formid').reset();
inside a conditional to ensure it only is performed for FireFox browsers.

Give it a try.

Resources
Prototype API Docs
Prototype's Form.reset() function API Docs

Thursday, July 19, 2007

Status of Safari and some comments about JavaScript and AJax

Sorry to say, but I have not been working with Safari for a while now. I did get a few comments pointing out some flaws in what I have done in the past. I am not too surprised, but luckily that is one of the best ways to learn and grow. I'll be activating those comments shortly.

As some background, I have tended to keep away from JavaScript in the past. The same holds true today, but at a lesser degree. The primary reason was cross browser compatibility and the fact that sometimes the JavaScript can get quite complex when you start to include all of the variations for past browsers and future ones.

One thing that has been winning me over is the JavaScript tools that are focused on prototype.js and the use of AJax. It also helps that the advanced features in CSS 2.0 and what will be coming with CSS 3.0 specifications will help reduce the need for scripting.

I have continuously been on the hunt for a good JavaScript based code set that allows table scrolling. I found a bunch of bad stuff out there. I found a few good ones too. One of which actually does work with AJax. I am wanting to post reviews and links to these tools for they are hard to find, and I think the information will be beneficial. The tool that works with AJax utilizes prototype.js which is great, but I have some enhancements that I am working on that will be increasing the performance by about a factor of 10 or greater (I think. Need to benchmark.) when dealing with rowsets greater than 1000 when the will be needing to deal with 40,000+, probably up to 100,000 rows... I know... I don't write the specs, just follow them :-(

For now I'll provide some links...

References:


http://www.sergiopereira.com/articles/prototype.js.html
  • Excellent independent documentation on prototype.js!
  • Using version 1.5.1

http://www.tetlaw.id.au/view/blog/table-sorting-with-prototype/
  • Excellent! Uses prototype.js and the code is very clean and readable.
  • Works with AJax
  • Sluggish with 1000+ rows. I have some fixes in the pipeline that once I get them tested I'll see about getting the code changes adopted.
  • Using version 1.0 with Prototype version 1.5.1 instead of 1.5.0_r
http://www.litotes.demon.co.uk/example_scripts/tableScroll.html
  • Works great with IE5, IE6, IE7, FireFox v2 (Windows and Mac OSx), and Safari
  • Low impact. Just call the function with passing the ID for your table and it does the rest.
  • Problem? It does not work with AJax. Works great with static HTML tables. Honestly, the code is a bit cluttered since it does not utilize prototype.js so it has to handle so much more internally.
  • Another problem appears to be that it was last updated in 2004.
  • Sad, it has strong potential if it was not for the lack of AJax support.