views:

196

answers:

4

Hi all,

I'm developing sites using progressive enhancement implemented completely in jQuery.

For instance, I'm adding onclick event handlers dynamically to anchor tags to play linked MP3 files "inline" using SoundManager and popping up Youtube players for Youtube links, through $(document).ready(function()).

However, if the user clicks on them while the page is loading, they still get the non-enhanced version.

I've thought about hiding the relevant stuff (via display: none, or something like that) and showing it when loaded, or putting a modal "loading" dialog, but both sound like hacks.

Any better ideas? I feel I'm missing something completely obvious here.

Regards,

Alex

+2  A: 

Assuming you have used good judgement and people are falling for the non-enhanced version just because the delay is too long then I would use CSS to disable the controls. The CSS will load almost right away. Then using Javascript I would toggle the CSS so the controls are re-enabled.

However, my first reaction is that if the user can click it while the page is loading, then your page or connection is too slow. However, if this is seldom the case--less than 1% of the time--then you can shrug it off as long as the user can achieve his goal, that is listen to his music. I say this because once the users realizes that a better experience awaits half a second later, he will usually wait for Javascript to render and then click.

aleemb
Yes, but CSS + no javascript = sucky experience! I admit that's uncommon, but I use Noscript myself and would hate this behavior.Also, I agree that this is uncommon, but I'd like to cater for it if I can.
alex
+1  A: 

I take the opposite stance from aleemb regarding using CSS. If you use css to disable the controls, then anyone who has javascript disabled or is using accessibility software will be unable to use those controls without disabling your stylesheet entirely.

You could use a very small inline javascript right before the closing body tag to hide the elements via js really quickly. If it's inline and doesn't have to load external resources it will be very fast, generally faster than a user can click.

However, I do agree with aleemb that if your users are able to mentally process the page and make it to the control they want to click before your js is loaded, there's probably a deeper problem with the way your page is loading. Look into ways to decrease load time: compressing image files, gzipping html/css/js files, minify your javascript, combine images into sprites, etc.

Gabriel Hurley
+4  A: 

I haven't tested this, but you could try live. The thinking is that you could put your click handlers outside of document.ready so they get executed right away. Since live uses event delegation to achieve it's functionality, you don't really need to wait for the DOM to be ready, and any clicks that are made while the page is loading should still be captured by the event handler.

If that doesn't work, you could try putting the Javascript script tags directly underneath whatever they need to bind. It's not pretty, but it will pretty much eliminate the problem.

Paolo Bergantino
Is live entirely cross-browser and well-performing?If that's the case, this is the right answer. I knew about live but hadn't thought much about using it for this...
alex
Live is guaranteed to work in all browsers that jQuery supports: http://docs.jquery.com/Browser_Compatibility
Paolo Bergantino
A: 

I'd suggest following Paolo Bergantino's advise - event delegation is the way to go to avoid the problem altogether.

I had a similar issue where event delegation couldn't do the job - you can read about that here.

Christoph