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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.