Jquery: Running Events As You Leave a Page
June 29th, 2008I recently attended “An Event Apart, Boston.” While there someone asked for my thoughts on a jquery problem which intrigued me. Mostly I was interested because I couldn’t come up with a solid solution in the 5 minutes or so we had left before the next session started.
The troubling spot for me was executing an animation on a link click before redirecting to the href of the link. I’ve seen the $("#content").animate({ opacity:1}, 100); trick to add delays within a queue, but for some reason that doesn’t work when applied to the document.
My proposed solution actually uses settimeout() which is what was suggested by the person asking the question. My question to the greater community is: Is there a better way to delay a call to document.location.href?
Here’s my proposed jquery page redirect delay solution.
Zipped for easy consumption
One note: I’m using jquery to move the content box off to the left and then bring it back in, so that people without javascript enabled will still see the content, but it seems to add an annoying flash of content sometimes. To avoid this, just remove the jquery line $("#content").css('left', '-3000%'); and add left: 3000%; to your css file.