nauti nauti - 10 months ago 83
Javascript Question

Login Page like Google with working back button

I have an login page and if the user enteres its username a post request to the server validates the username and provides an appropriate response.
If the username was valid a new input field for the password appears. I want the user to be able to go back to the input for the username but because I'm using ajax and javascript to display the new input field for the password after the username was validated, the back button just skips the username input.

The behavior of Googles login page when one presses the back button after entering the email and is "redirected" to the password input is exactly what I want.

How can I achieve the same behavior?


Answer Source

When you enter email id the gmail url is like:

And when password:

Look the change is only the hash tag at the very end of the url. Now how 'll you implement this?

There is a event in javascript called "hashchange".

Take a look at this:



    <div id="uname">
        //username related code

        <a href="#password"><div id="hello">Next</div></a>

    <div id="password" style="display:none">
        //username related code

        <div id="final">Submit</div>



                window.addEventListener("hashchange", function(){                                               //this is the trick
                        console.log("Hash changed to", window.location.hash);

                            //show password related div
                            //show username related div