The technique behind the jQuery UI demos10

I’ve decided to start blogging more about the actual things I’m working on everyday, and in this series, I want to first talk about the updated and improved demos of jQuery UI. The first important thing to understand is that the demos that come with the development package, and that you can browse from the trunk, and the demos you see at the jQuery UI website are the same.

The standalone demos

To me, the new demos are truly outstanding. Let’s talk a bit about why. If you download the development package, or browse the trunk, you’ll see a demos/ folder, with sub directories for every individual plugin. If you open one of these sub-directories, you’ll see a index.html, a default.html and named individual demos, i.e. containment.html. Each of these demos:

  1. Is a standalone demo
  2. Includes all the files it needs (JavaScript / Theming )
  3. Displays a description of the current demo at the bottom

This means that those demos are excellent for learning how to do something – they come completely uncluttered, so you can copy and paste JavaScript and markup easily. Additionally, the default.html shows you the most simple state of the plugin (usually without any provided options), and the index.html provides a easy named listing to all these sub demos. Even better, if you open demos/index.html, you’ll see a page that looks very similar to the website demos section, but works like this:

  1. If you click on a plugin in the left hand navigation, i.e. ‘draggable’,
  2. it fetches the content of demos/draggable/index.html using Ajax and displays it as right hand sub navigation
  3. if you click on one of those links, the created iframe in the middle updates itself

The website demos

For the website demos, it got a bit more sophisticated. We removed the beautiful but unusable demo carousel, and created a unified demo section that automatically pulls the standalone demo files from the repository, therefore it’s never out of sync. However, the simple iframe approach couldn’t be used because of the incredible amount of server requests made that way. Here’s the technique behind it:

  1. include the theme and jQuery UI once
  2. include demos/index.html using PHP, filter out any script/style tags and the header
  3. now using JavaScript, rewrite all links to plugins to point to /demos/plugin

This shows you the index file. Perfect. What happens if you click on a link in the left hand navigation now?

  1. A server-side rewrite rule resolves demos/draggable to demos/?load=draggable
  2. in PHP:
    1. we export the ‘load’ information to Smarty, so we can access it in the template
    2. we then retrieve default.html and display it in a div frame, but filter out any script/style tags and header
    3. and we do the same for draggable/index.html, to show the right hand sub navigation
  3. in JavaScript:
    1. we loop through all demo links in the right hand nav and define click events,so the link isn’t triggered, but instead, a hash is set (for easy bookmarking) and
    2. the selected file is pulled in via ajax – again, we filter out any script/style tags with src attributes, and the header/footer, and update the frame div
    3. at last, we pull out the description at bottom of every file, and display it below the ‘frame’

So, with a little more work involved, we not only made the demos extremely responsive and fast, but also reduced the requests to the server dramatically. Every time you click on a demo, only one single request is made, to fetch the actual html page.

Pretty cool eh? Enjoy!

10 Comments

pd  on January 2nd, 2009

I really appreciate this detail, like the new weblog theme etc. There’s just one problem: most of the demos for the slider don’t appear to actually work:

http://jquery-ui.googlecode.com/svn/trunk/demos/slider/index.html

that is, unless I am missing something gob-smackingly obvious. I’ve enabled

Paul  on January 3rd, 2009

@pd: They all seem to work fine for me – try to clean your cache and try again. If they still don’t work for you, leave me another message. Thanks!

Wayne Khan  on January 6th, 2009

Hello Paul,
I got to your site from the jQuery UI docs page, rather than the other way round.

But anyways, there’s a problem with the “Easing” effect:

http://ui.jquery.com/repository/tags/latest/demos/functional/#ui.effects.easing

When I click the bounce button, the picture animates, and then I am redirected to a broken URL:

http://ui.jquery.com/download_builder/

It’s probably got to do with this tag:

I use Iceweasel/3.0.4 on Debian Linux.

Thanks alot! I love jQuery UI by the way! :)

bh  on January 6th, 2009

The new ui themeroller demo.html is not functional – none of the components respond as expected when clicked. Should it be functional? Tested in both FF 3 and IE 7.

bh  on January 6th, 2009

Ok, it seems that I’m getting a 404 for the js stuff at http://ui.jquery.com/js/ (jquery.js and ui.js). Linked locally to jquery.js and jquery.ui.all.js and things seem to be ok for the 1.5 stuff (1.6 still not working for me). Great detail on the demo code in this entry, thanks.

bh  on January 6th, 2009

One more comment on the 1.6 themeroller and I’ll leave it be – some of the element id’s are off – i.e. the accordion div has no id, and inline script is missing when compared to 1.5 demo.html. Thanks Paul and please forgive the multiple comments.

Paul  on January 15th, 2009

@bh that stuff should be fixed by now, best is to inform us via our jquery-ui google group or a ticket in the bugtracker.

@Wayne Khan: the functional demos have been completely revamped. All of these issues should be fixed by now.

felix  on January 27th, 2009

Ein großes Lob an dich!
Ich programmiere gerade eine größere Community, in der ich sehr viel JQuery nutze.
Wüsste nicht, was ich ohne diese Erleichterung täte!
Frohes Schaffen weiterhin und beste Grüße

JJ  on April 15th, 2009

Hi Paul,

I’m sorry for OOT, I have a question is there possible to use more than one instance jQuery UI dialog in one html page??

thx

Useful jQuery Documents and Plugin | Beat Fly Blog  on July 9th, 2009

[...] jQuery UI Demo. The first important thing to understand is that the demos that come with the development package, and that you can browse from the trunk, and the demos you see at the jQuery UI website are the same… [...]