8

I am working on a project in which i need to handle browser back button and on it's click I want to redirect to home page.

I have gone through many post, there are a lot of ways to implement it.

I am wondering, what is the best way to implement it.

If anyone knows similar post, please post the url.

Thanks for help

I have used the following codes:

function HandleBackFunctionality()
{
    alert('ckikc')
    if(window.event)
    {
        if(window.event.clientX < 40 && window.event.clientY < 0)
        {
            alert("Browser back button is clicked...");
        }
        else
        {
            alert("Browser refresh button is clicked...");
        }
    }
    else
    {
        alert('clicked')
        if(event.currentTarget.performance.navigation.type == 1)
        {
            alert("Browser refresh button is clicked...");
        }
        if(event.currentTarget.performance.navigation.type == 2)
        {
            alert("Browser back button is clicked...");
        }
    }
}
window.onbeforeunload = HandleBackFunctionality;
$(document).unload(HandleBackFunctionality);

well its not working for me :(

well @kapa said it is a duplicate question, I have mentioned that have googled a lot for this and have seen a lot of solutions for this problem.

my question is not the way to solve it, but the best solution out of available one

Anton Rand
  • 322
  • 5
  • 20
rajansoft1
  • 1,336
  • 2
  • 18
  • 38

1 Answers1

11

You can't just override a browser's button's behavior.

Imagine the security problems it would cause if anyone could just "Fake" browser navigation like that. Making users think they navigated to a different site is chapter 1 in phishing for dummies...

However, @Alek linked to a similar question, in the comments on your question, which has a decent alternative (Which is, imo, the best way to achieve the desired result).

Community
  • 1
  • 1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 5
    I think OP wanted, redirect to homepage if back button clicked of browser. – Jai Jun 04 '14 at 09:00
  • 1
    Yea, I got that, but that's not possible. Even if it were, it shouldn't be done. Don't attempt to change a browser's functionalities. If I visit `www.a.com/b`, then navigate to `www.a.com/c`, I expect the back button to load `www.a.com/b`, not `www.a.com` – Cerbrus Jun 04 '14 at 09:02
  • 7
    @Cerbrus we do it in single page app too, so it should be there if we need it – rajansoft1 Jun 04 '14 at 09:07
  • 1
    @rajansoft1: The question I linked to in my answer, has a answer that explains how single page apps do it. That way has nothing to do with changing the `back` button's functionality, it rather changes the way the site is "registered" in the browser's history, without messing with the browser itself. – Cerbrus Jun 04 '14 at 09:08