Back a million years ago, I wrote about using Ajax to allow visitors to search my site. When I ported the Web site from MovableType to WordPress, I had to give up this feature because I didn’t know PHP or WordPress well enough to make it work.
This weekend, I put the finishing touches on a new design for the site I share with my wife and I was finally able to bring back the Ajaxy goodness.
In addition to making the Web site a little more visually interesting (after a long period of boring design), the new theme supports my lightbox library for forms. Lightboxes seem to be all the rage for displaying popup enlargements of thumbnails on a Web page. The idea is so terrific that I had to apply it to popup forms (AKA dialog boxes) that communicate back to the server using Ajax. Below is the search box:
You’ll note the translucent overlay that’s the hallmark of the lightbox technique, however, instead of displaying an image, I’m displaying an Ajax-enabled search dialog box. Type your search terms and hit enter. You’ll get the now ubiquitous Apple spinning marker followed by (we hope) some relevant search results.
This is all so easy to do that I quickly popup a contact form together to take further advantage of the library:

Of course, this wouldn’t be that nerdy if I were only going to brag about some new features. Let’s look at what’s required to make the lightbox go.
First you’ll want to include all the necessary scripts in your page:
<script src="helpers.js" type="text/javascript">
</script>
<script src="lightbox.js" type="text/javascript">
</script>
<script src="scriptaculous/prototype.js" type="text/javascript">
</script>
<script src="scriptaculous/scriptaculous.js" type="text/javascript">
</script>
These should be included in the page that contains the lightbox (although it should be safe to include them in the lightbox content page as well, which makes the dialog work stand alone). Then add a rel="lightbox" attribute to any links which should open in the lightbox.
The page that displays in the lightbox needs a little tweaking to work perfectly. If you want the dialog page to work as an independent page, you should consider adding an ID of lightboxContent to the body tag. This tells the lightbox library that when it loads the page via a prototype Ajax.Request call, it should discard everything but the body (and any custom scripts you’ve declared).
Next, you should tell the lightbox to manage your form. In this case, we’re working with the search dialog’s form searchForm.
lightbox.manageForm( "searchForm", searchComplete, beforeSearch );
lightbox.setMayClose( true );
The call to manageForm includes the name of the form (because it may not have been created yet depending on how your browser renders content) and several event handlers: one for when the form is submitted and a response is received, and the other for setting up and validating the data just prior to submitting the form.
The second call is to setMayClose which tells the lightbox to display the little close box in the top right corner of the dialog. This should only be enabled if it is safe to dismiss the dialog box at all times. If you’re asking the visitor to make a choice between a 15″ MacBook Pro or a 17″ MacBook Pro, it doesn’t make sense to include a close button, because what’s the choice then? Instead include a button to cancel the entire process.
Of course, what you do with this technique is up to you. For a site I’m designing for my employer, we’re tossing JSON formatted data around. But for Newburyportion.com, I’m just passing HTML.
Feel free to download the source and play with it. You’ll want the latest Scriptaculous library to make it all work.
9 Comments
any info on how i can get my lightbox to load when the page loads?
i’ve seen a lot of things done with these , but they’re all happening when you Click for them…
thanks!
Felipe, well there’s a really easy way to do this: Trap the page load. You’re already using Scriptaculous & prototype if you’re using my lightbox. So just use a page load handler.
Of course, this will only display the lightbox after all content has been loaded — every image, every
iframe, every thing. There are some tricks you can use to discover when the document has loaded the DOM before loading images and the rest of the content, but I’ll leave those for another post.cant quite get this to work.. mmm. very nice looking though..
hungry: do you mean here on the site or on your own?
Jeff
Mucho gracias for this implementation. I have implemented it in my new design I just put it place. Credits are give as well (using the same functionality!)
Your forms and text from your dialog are showing up, but no borders, background or overlay… Did you misplace your images?
Pretaul, excellent catch. When I did a complete redesign recently, I must have left those out. Thanks.
Jeff, I can’t find the javascript files to download. Am I blind? Perhaps you could email them to me, cheers.
Hehe, Yeah, I basically did a more sophisticated version of this for a commercial webapp using scriptaculous and prototype. I wrote a whole ‘javascript window manager’ that lets you put up div based ’subwindows’, attach chrome to them, drag and drop them around, etc. It will even manage sub-sub-windows, closing dependent views, etc. You can also tell it to use real windows instead of divs. Overall the concept is pretty cool.