Script.aculo.us: How to easily AJAX your site
Script.aculo.us is an amazing framework for enriching your website's user experience very easily. Built on top of the very popular prototype javascript framework, scriptaculous makes AJAX cake, and gives everyone the power to have a sexy AJAX-powered website, yes everyone. Scriptaculous is a tiny (140kb) javascript framework that gives you the power of effects, several dynamic data components, and all you need to know is a little bit about HTML and how to read. If you already have your own website, it is not hard to do. Follow these steps to AJAX up your site in no time:- Download script.aculo.us. Click on any of these links to begin downloading the zip, tar.gz, or tar.bz2 version.
- Unzip the archive somewhere, preferably somewhere you will find it again. This is key.
- Upload the whole thing to your web server, and put it where ever you like, again, it should be somewhere you can find it. Once again, finding it is key. You will need to link to these files from your web page, so putting the files in a sub folder of your home directory (root for you unix junkies) is a good idea.
- In the header section of your web pages, you need to put this code:
As you might notice, the code has to point to your uploaded script.aculo.us files. You would replace the "ajax/" portion with your own path depending on the name of your subfolder.<script src="ajax/prototype.js" type="text/javascript"></script>
<script src="ajax/scriptaculous.js" type="text/javascript"></script> - Next, put the one line of code into your pages or posts where ever you want the effect to take place. Let's say you have a content div element you want to have all the sudden appear in the page, all you do is wire the "SlideDown" effect (one of many in script.aculo.us, download the cheat sheet here) to a button, link, or other clickable element on your page. The code to trigger the SlideDown effect from a link looks like this:
<a href="#" onclick="Effect.SlideDown('content');">SlideDown Now!</a>
Why use an <a> tag? First, it is super simple to code (everyone knows it), and second, users think it is an ordinary link. They will be quite surprised when they find out it isn't normal at all. I call this "hacking old-skool users into using web 2.0 unknowingly" (or something like that). - Anyway...be sure your div tag has the id="content" because that is the name of the element the SlideDown effect will be applied to once you click the "link" we just made. Make sense? Good. NOTE: Something to remember is that your DIV will be shown on the page automatically by default, unless you first hide it, so the visual magic of an effect such as SlideDown can really be displayed. To "hide" our div by default so your content won't appear until you click the link we just made, you will need to add an inline CSS style (or add one in your CSS file, if you have one) so the content div ends up looking like this:
You'll notice (I hope) that the underlined code is the part that hides this div by default. When the SlideDown effect is triggered by clicking on the <a> link, it will automatically change the div's display property, so don't worry about having to do that.<div id="content" style="display: none;">This is the content.</div> - Try it out, and have fun! It should slide down your content, which can be anything you put in that div tag, video, audio, pictures, more javascript, an iframe, whatever, use your imagination. There are tons of different effects you can use, so you should check them out at script.aculo.us's website, since they have great demos and even combination demos for different sets of effects.
Since the script.aculo.us wiki is large and contains a lot of information to consume all at once, I have compiled a basic script.aculo.us effects cheat sheet of sorts for your quick and dirty coding enjoyment. You can download my PDF cheat sheet here.
NOTE (to lightbox and TinyMCE users): There are some special instructions on integrating script.aculo.us with both of these other frameworks, since they all use Prototype to some extent. You can find TinyMCE help here (script.aculo.us wiki) and most lightbox frameworks list this info on their support page or forums section of the site. Have fun!












Comments
33
Subscribe to commentsSergioDec 30th 2006 12:46AM
What a coincidence. I've just spend a good part of today updating the prototype docs page that I keep (already mentioned in another comment) with a few v1.5.0 goodies.
Enjoy:
http://www.sergiopereira.com/articles/prototype.js.html
ShibathedogDec 30th 2006 12:46AM
haha i like how ryan pwned that first guy
nice writeup though, ill have to try this out whenever i get around to setting up my server again
Don WilsonDec 30th 2006 10:16AM
BTW, Ryan, you suggested that scriptaculous is only 4kb, forgetting that you need to include Prototype as well, which is rather large (50kb alone), so yes, the first poster was correct.
Twenty5Dec 30th 2006 1:43PM
SlideDown Now!
so..... what should i put in the "#" ?
Ryan CarterDec 30th 2006 1:38PM
Actually, you just put #, since the tag doesn't go anywhere. It is just used as an onclick event holder. It is a lot like using javascript void.
Twenty5Dec 30th 2006 3:17PM
..... i dont get it.... mine is not working....
Sorry Ryan, im kinda new on the AJAX thing... any suggestions?
Ryan CarterDec 30th 2006 3:24PM
Generally, if it isn't working, your javascript declarations in the header section contain a typo or don't actually point to the correct place to find the files (scriptaculous.js and prototype.js). Another common problem is putting the prototype.js line AFTER the scriptaculous.js one. Prototype must come first or it won't work. See my code in the cheat sheet for a full html example that should work.
m.a.Jan 1st 2007 12:16PM
My main problem with all of this is, that it does not downgrade gracefully. Scriptaculous does not work very well on the Opera Browsers (at least last time I looked it did not, which was beginning of december) and falls flat on the face if someone turns javascript off.
As long as I don't find a solution where even with javascript off I will get at least a rudimentary solution of displaying it (and hardcoding "display:none" into it won't do) I'll keep my hands of scriptaculous and co, cool as they may seem.
Joe SmithJan 3rd 2007 2:27AM
Dude... AJAX? Where's the ajax? All I see is you using an effect. This is the most useless post I have ever seen. I am now dumber for reading it and may God have mercy on your soul.
Ryan CarterJan 3rd 2007 12:35PM
Alrigght, I will say it one more time for those who missed it. Yes, I agree, it is NOT technically AJAX, it is merely Javascript effects. Scriptaculous is however an AJAX framework, that yes, does real AJAX (or Ajax) work (more to come on this later). "AJAX" is the best thing to title this post so all the n00bs understand it. I had to start somewhere, like the easy stuff, effects. N00bs think AJAX is "pretty effects", so my intent is to re-educate them little by little and help readers understand the quick and dirty way that Scriptaculous can give you AJAX functionality without all the hand coding and ridiculous hoops you normally have to jump through. There, I said it, make sense?
ryanJan 8th 2007 1:50PM
This is not an example of Ajax. Adding Javascript does not make something Ajax. This is just an effect example of Javascript.
AlexJan 13th 2007 12:56AM
So is this AJAX or not? There seems to be an unresolved debate here?
Ludvig EricsonJan 21st 2007 4:01PM
People like you make me angry.
AJAX does not have a clear meaning. It is said to be asynchron JavaScript combined with XML (Asynch. JavaScript And XML) and script.aculo.us is about interfaces. While this may or may not be using XMLHttp is irrelevant, because the library itself is meant to spice up interfaces.
AJAX, as I said, requires the use of XML. It's in the acronym. Today most people use JSON, because XML is a bloated format.
AJAX is a technique. It is not a verb.
Root is not each user's home directory. Root is as the name says the bottom of a chain, like a directory tree. Root is also the superuser on POSIX compliant systems.
Oh and most of what I said is already commented, and indeed, script.aculo.us is everything but lightweight.