views:

30

answers:

3

Hi All,

I am new to javascript programming. I have a page on which if Back button is clicked, I want to redirect to a new location.

Is this possible? I have searched for this & I guess onbeforeunload() can do this, but i didn't get it.

Regards.

A: 

The would be a real surprise for your users to see that the back button don't send them to the preceeding page but to a new one.

Could you explains why you would need to do that, they may be other solutions that changing the behavior of the back button.

Loïc Février
This is more of a comment than an answer, isn't it?
T.J. Crowder
Yes, if I was allowed to comment I would have done it...but at the time of my answer no one had ask him what was the purpose of this.
Loïc Février
@Loïc: Ah, yes, the limit where you need at lest 50 rep to leave a comment on someone else's question or answer. I just searched your answers and it didn't take long to find one to vote up, so you'll be able to comment in the future.
T.J. Crowder
+1  A: 

It's possible to hook into the browser navigation buttons, although doing it reliably cross-browser is tricky. Your best bet is to use a library that's already done the hard work for you. There's Really Simple History, for instance, which isn't reliant on any particular JavaScript library or framework. Most of the main JavaScript libraries out there (jQuery, Closure, YUI, etc.) either have history support built-in or plug-ins for it. These libraries help improve the user experience you provide when you're using in-page navigation.

Just for what it's worth, and perhaps a bit off-topic: You only want to hook into the Back button if you're helping ensure that it does what the user expects. For instance, you wouldn't want to take them to something they'd never been to before when they clicked the Back button, that would be ... I'll go with "inappropriate." :-) And you certainly don't want to interfere with the Back button leaving your page if the user has just come to it from somewhere else. But there are perfectly valid scenarios where you may need to hook into the process. For instance, if you provide a tabbed interface within a page that you handle by changing the visibility of divs, it would be reasonable to ensure that if the user is on Tab 1 and clicks Tab 2, then clicks Back, they end up on Tab 1 again.

T.J. Crowder
A: 

Please do not implement this. This comes across, no, this is a user experience nightmare.

The browsers back button is indended to take the user back to the previous page. That is its only purpose. I cannot think of a reason why you would want to get rid of this handy functionality.

OK, now that I have got that out of my system. You can either look at T.J. Crowder's solution. Or you could possibly implement something like the following instead:

You could open a new window via the window.open() with the location you are after and close the current window.

Russell Dias