views:

1955

answers:

7

I would like to use the jquery slideUp effect when the user navigates away from the page just to make the page look cool as it closes.

I assume that I should use the onunload event but how can I delay the page closing long enough for the effect to run to completion.

One of the options that came to mind is effectively hijacking the page closing function, storing it in some variable and then executing it once I had run my effect but I have no idea how I would do that.

Any suggestions or alternative ideas are more than welcome

+3  A: 

what you're looking for is the onbeforeunload event.
just a warning though... it should be really quick... or your visitors are probably going to hate it no matter how cool it looks

as to preventing the page from navigating away before the animation is done, that's a bigger problem... any animation is going to rely on setTimeout/setInterval and the page won't wait for those to complete before leaving.

Jonathan Fingland
my point exactly!
balexandre
Good stuff - have an upvote :)
alex
Even if he wanted to use the onBeforeUnload event it wouldn't work since animations require delayed action - the onBeforeUnload handler is expected to return something straight away.
J-P
@J-P yes and no. The onbeforeunload handler does not have to return something *right away*. For example, you can do a synchronous ajax request, or a really long while loop inside the onbeforeunload handler.... just as long as you don't go over the timeout for scripts running too long. That said, I wouldn't recommend doing so.
Jonathan Fingland
+1  A: 

why not, instead of making a "cool" effect when a user simple want to go away from your website (even if the user closes the browser/tab the unload event will be fired) and annoying the simple user with that ... preventing him/her to return again...

...do that "cool" effect when a user reaches your website for the first time? as a normal intro effect?

I did that as a simple idea, you can see it here: http://www.balexandre.com/jmfc

balexandre
I want to do the effect as pages are navigated within my site, so the user wont be annoyed because the page is just loading new content. Upon loading the new page I'll use the slideDown effect and it'll look like the information in the content part that slided was just altered.
j3frea
So I will also want to check where the page is going to decide whether to do the effect
j3frea
depending on content, why not using a slidder for such things? and a nice and normal loading message for the rest, like "We're getting all the bits to produce this page you want to ... almost there... there! Enjoy."
balexandre
+1  A: 

I would agree 100% with Jonathan Fingland's answer, and add this.

In IE, (I'm not sure what versions support this, I know IE6 did) you can use some propriety meta tags to achieve fades etc when leaving the page. However, this is limited in browsers (IE only), so you're stuck for cross browser use.

alex
+1 I was going to say just use Transitions nice.
bendewey
+3  A: 

Doing anything but closing the window when the users ask to is breaking a contract with the user. The browser window is not yours, it's the users, and no matter how cool the effect, it will inevitably annoy most of your users.

The onbeforeunload event is very restricted in what it can do. It must return a string, which is then used to prompt the user for a confirmation about leaving the page. It won't work for cool animations.

Magnar
+2  A: 

As far as I know, the only way to stop a user from leaving a page is the onbeforeunload event, which isn't cancelable. Instead, you return a string from that method, the browser prompts the user with a Yes/No dialog, life goes on. 276660 has more info about this.

I don't think you're going to have much luck with this one.

Dan F
A: 

The only way I've found for delaying the window to close, is using an alert. If this is an acceptable compromise for your case, it will really delay the window destruction in memory, and allow your page timers to execute (of course, if user does not close the alert popup earlier than your animations finalize).

I recently used this approach so i could call a flex method through FABridge (which would otherwise be destroyed before the flex method call finishes). I'd like to hear your comments on this.

alex
The idea was to have a seemless feel to the page so an alert wouldn't really be appropriate but thanks for the thought.Also, I'm not sure that an animation will run in the background of an alert, I'd have to try it but I suspect that it may not...
j3frea
+1  A: 

You may find loading new content via AJAX would give you better control of effects and transitions, as well as reducing the annoyance factor to the user which can result from trying to hijack the browser actions in such a manner.

I would look at using a form of slider as mentioned above (see for instance http://webdesignledger.com/tutorials/13-super-useful-jquery-content-slider-scripts-and-tutorials), or simply loading content panes in response to user clicks.

Luke H
I would also add that, for the sake of accessibility, this should be done by hijacking real links rather than a JS-only system
Jonathan Fingland