开发者

Javascript redirect when clicked on back button

开发者 https://www.devze.com 2023-01-17 15:42 出处:网络
I am new to javascript programming. I have a page on which if Back button is cl开发者_如何学运维icked, I want to redirect to a new location.

I am new to javascript programming. I have a page on which if Back button is cl开发者_如何学运维icked, 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消