I often talk to people who haven't worked with one of the cool javascript frameworks out there. I am partial to prototype and scripaculous, but I also really like MochiKit.
If you aren't taking advantage of one of these, then perhaps this post is for you... I give you the ultra quickstart to diving into prototype and scriptaculous!
The best thing ever invented since rubber tires: a replacement for document.getElementById('control_id');
You know you're not supposed to use document.all or document.ctrl_id, right? Well at least now you do. document.getElementById() is the cross-browser safe way of getting an element off of the page. The prototype equivalent is $('control_id'). This will automatically extend the element returned with some helpful utility methods (like extension methods in C# 3.0). -- more on this later.
document.all
document.ctrl_id
$('control_id')
Along the same lines is a useful method for selecting elements using CSS selectors. If you're a CSS junkie (like me!) you'll appreciate this.
$$("div span.info");
Want to show or hide an element? Use Element.hide(ctrl), Element.show(ctrl), or Element.toggle(ctrl). You can attach these methods onto the element itself by saying Element.extend(ctrl). If you retrieved it via the $() method, then your object already has these methods.
Element.hide(ctrl)
Element.show(ctrl)
Element.toggle(ctrl)
Element.extend(ctrl)
Let's say you want to grab all of the span tags from a parent div tag. Typically you'd do this:
var spans = parentDiv.getElementsByTagName("span");
but what is spans? If you thought it was an array, you'd be wrong. It's a NodeList, which is not nearly as powerful as a javascript array. (With javascript arrays you can push(), pop(), shift(), and other cool things that you can't do with a NodeList. Anyway, back to the spans collection, we can elevate this to an array by using the $A( spans ) method. Now we can use all of the nice array goodness for that list.
$A( spans )
How about fading an element?
new Effect.fade(elem);
Want to make a slide out box?
new Effect.toggle(elem, "slide");
This will toggle the element using slide up and slide down. ("fade" and "appear" also work here for toggling).
Forget UpdatePanels. Most of the time we just need to fire off a request and get a list of values back from the server. Prototype has you covered there:
new Ajax.Request( "/path_to_server/ajax/AjaxHandler.ashx?op=getUsername&value=bob", { method: "get", onSuccesss: function(result) { if(result.responseText) alert('available!); else alert('taken'); }, onFailure: function(e) { alert('something went wrong ' + e); } }
I don't know about you, but that's pretty painless to me.
Want something a bit richer? How about an autocomplete textbox? I wrote this one to auto-complete cities as you type.
var url = '~/AjaxHandler.ashx?op=get_cities';ac = new Ajax.Autocompleter(city_textbox_id, results_id, url, { paramName: 'filter', frequency: 0.2, minChars: 1, indicator: indicator_id, callback : function(element, filter) { var state = $(state_dropdown_id); var state_code = state.options[state.selectedIndex].value; return filter + '&state=' + state_code; }});
That's it. I'm not kidding. I handed it a textbox, a div to display the results, and a url to get the data. It takes care of all of the ajax requests, the frequency limiter to prevent us from firing a request with every single keystroke, and returning the results into a list. It even has full keyboard support. The url looks like ~/AjaxHandler.ashx?op=get_citiees&state=TX&filter=Hou and it will return the matches that it finds (based on an HttpHandler I wrote).
What are your favorite prototype / scriptaculous protiops?
I'm Ben Scheirman. I am a .NET software developer with a strong interest in agility. I work as a Principal Consultant with Sogeti.
Read more here.
email me
Ads by The Lounge
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.