Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change back button functionality in browser using jQuery

Is it possible to change the address of the back button in the browser? For example I am in this page:

http://website.com/page3.php

I submit a form and it returns this address:

http://website.com/page3.php?ok=ok

After which when I go to another page, for example

http://website.com/blog.html

and click the back button, it goes back to the page with $_GET variables on it.I want it that when I click the back button of the browser to go here:

http://website.com/page3.php

Is it possible for the back button (of the browser) that everytime I click it whenever I just came from that specific page with variables, it wont get the variables as well?

like image 287
marchemike Avatar asked Oct 02 '22 11:10

marchemike


3 Answers

I think you can set a session variable on your form in http://website.com/page3.php. and then unset it once the submission is done in http://website.com/page3.php?ok=ok.

and each time the page with the variable in the url is loaded, check immediately for the submission session variable to see if you are redirected to the page from the form page. and if not, redirect you back to the page3.page.

I hope I made myself clear enough!

like image 156
Reza Saberi Avatar answered Oct 13 '22 09:10

Reza Saberi


This is not possible you can hack something together by using pushState and playing with the browser history

like image 24
jayaguilar Avatar answered Oct 13 '22 11:10

jayaguilar


One method would be to do the form post request using AJAX.

Another one would be to add tokens to your forms and check them server side to redirect using header() if they don't exist. Eg :

<?php
    session_start();
    if(isset($_GET['ok'])){
        if(isset($_SESSION['form_page3_token'])){
            $_SESSION['form_page3_token'] = null;
            //first request : do your stuff
        }else{
            //repost (F5 or Back in the browser => redirect to origin form)
            header('Location: http://www.website.com/page3.php');
        }
    }else{
         $_SESSION['form_page3_token'] = session_id();
         //here the code to show the form
    }
?>
like image 38
Loïc Avatar answered Oct 13 '22 09:10

Loïc