When back button clicked redirect to URL?

How to do this in jQuery?
When someone clicked the back button on the internet browser.
It will redirect it instead into a particular URL.
Can you guys show me the codes?
How is this done?

Thanks in advance.

Hi there iridion9,

if I clicked the back button on my browser and ended up in your
back yard I would assume that my p.c. had been hacked. :cold_sweat:




The “Back” button has a specific function. You should not hijack the native browser controls for any reason.

Presumably the person who used the button wanted to return to the previous page, and they should be able to do so. There are very few exceptions to this. Using the “Back” button on my bank’s Internet banking service shows a “sorry - this document has expired” message, and logs me out, for security reasons. (And there are clear warnings on the site not to use the “Back” button.)

What are you trying to achieve? If we understand the circumstances, we might be able to suggest a better solution.


I don’t think there’s a jQuery functionality for this. However you can use the vanilla JS history.pushState() or history.replaceState() methods – as always, have a look at the MDN for details. And then, don’t use it. ;-)


Actually I’m trying to create a series of questions.
The test/quiz must go forward and don’t allow the user to go back to the previous question.

My purpose is when a user clicked the back button it should display a message.
This message is in .php page.

So can you show me the codes for this in jquery?
Any good solution for this?

Thanks in advance.

If you track the progress on the server via a session then you can have pages where the questions have already been answered disable the inputs. It may even be possible to have it work out where they are up to and jump to the appropriate page.

Can’t do anything client side though so there is no jQuery code involved.

So why wouldn’t history.replaceState() do the trick? Maybe you could use it in a more user-friendly way by not actually replacing the URL with the location of your PHP script, but just passing a state object like

window.onpopstate = function(event) {
    if (!event.state) return;

    if (event.state.visited) {
        // Send an XMLHttpRequest to your
        // page.php and display the response

history.replaceState({visited: true}, '');

Or you could write the whole thing as an SPA where each new question gets fetched via AJAX, so there wouldn’t (necessarily) be a back button at all. If you want to use jQuery by all means, have a look at $.ajax(), which provides a convenient way to communicate with your server.

I cannot show you anything with “JQuery”, as I tend to
avoid all “Frameworks” like the plague. :mask:

As “Ajax” has been suggested by various members as
a possible option I can show you an example of that. :ok:

Check out the attachment to see it. :sunglasses:

ajax.zip (3.7 KB)

Obviously, you will need to use a server for it to work. :ok_hand:


1 Like

That’s JavaScript and anyone wanting to go back can bypass it by turning JavaScript off.

1 Like

Yeah, using javascript and mix with jQuery is fine.
as long it works.

Hi I have attached sample files.
But it is not complete.
Can you help me tweak this script so that it will redirect to a particular .php page?

thanks in advance.

back_one.html (663 Bytes)

back_two.html (145 Bytes)

which you can’t guarantee as anyone wanting to cheat your system will turn that particular script off.


Not a problem.
If they turn it off. they are not fooling the system.
It is their selves they are fooling…

Once this system is done, I’ll show it here.
So everybody can see and have the experience to try it.

So any one plase help me with this?

thanks in advance.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.