How to set control go back?

Hi all,
I used to alert the message by using the java script, when user is not logged in, and wants to access the page. At that time i used to alert the message box, u need to login to access the page…
Then user clicks on ok button of alert message box, it goes to the login page…

Now the problem is,

After successful login of a user, how to set the control to show the earlier page he wants to access.(i.e without login, before the page).

If the user directly goes to the login page, after login, we will show the normal user home page…

How to do this?

Give me the example, to implement it…

Thanking you for your reply…

You need to save the page id that the user was going to view when your JS alert was triggered. save it in a session variable, then as soon as the user has logged in you can redirect them to the page held in the session variable.