2

I have the following code:

function get_login()
{
    hideshow('loading2',1);
    error(0);

    $.ajax({
        type: "POST",
        url: "The URL",
        data: $('#logins').serialize(),
        dataType: "json",
        success: function(msg){  

            if(parseInt(msg.status)==1)
            {
                succ2(1,msg.txt);                   
                setTimeout('go_to_you_page()', 1000);

            } else if(parseInt(msg.status)==0) {
                error2(1,msg.txt);
            }

            hideshow('loading2',0);
        }
    });

    return false;
}

function go_to_you_page()  
{  
    window.location = 'myaccount.php';     
} 

If the user is not logged in, it will add index.php?redirect=inbox.php (for example)

How can i redirect the user after logging in to the /inbox.php ?

Thank you in advance!

Giordano
  • 5,422
  • 3
  • 33
  • 49
Omar Masad
  • 155
  • 1
  • 4
  • 18
  • What exactly is the question? You seem to have the proper code to achieve a redirect based on a link. – Khez Apr 10 '11 at 23:39
  • Set the window.location in your success handler or reply with a response redirect in your XHR reply. – lsuarez Apr 10 '11 at 23:41

1 Answers1

4

Here's some code to get a URL parameter:

function get_url_parameter(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if ( results == null )
      return "";
    else
      return results[1];
}

You can use this in your go_to_you_page() function:

function go_to_you_page()
{
    var redirect_parameter = get_url_parameter('redirect');

    // redirect to /myaccount.php if the redirect parameter is not set
    var redirect = ( redirect_parameter === "" ) ? 'myaccount.php' : redirect_parameter;

    window.location.href = redirect;
}
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • 1
    Correct answer but according to a post [here](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript) window.location.replace is better to use than window.location.href .Check it out – human.js May 25 '12 at 08:52