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!

2
  • What exactly is the question? You seem to have the proper code to achieve a redirect based on a link. Commented Apr 10, 2011 at 23:39
  • Set the window.location in your success handler or reply with a response redirect in your XHR reply. Commented Apr 10, 2011 at 23:41

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Correct answer but according to a post here window.location.replace is better to use than window.location.href .Check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.