smrkem
smrkem

Reputation: 173

window.location not working in ajax form submission callback

I have forms (register and login) and modal background that gets added to the page if a user clicks the Login button. I'm submitting the forms via ajax using jquery.

$.post("/login",{
    "ajax":true,
    "email":$("#popup-window #login-form-email").val(),
    "password":$("#popup-window #login-form-password").val()
    },function(response){
        console.log(response);

        if(response.message=="success"){
            if(response.user.type==1){
                        console.log(window);

                        // window.location = "http://www.whatever.com/admin";
                        window[0].ownerDocument.location.href="http://www.whatever.com/admin";
                    } 
            if(response.user.type==2) window[0].ownerDocument.location.href = "http://www.whatever.com/trades";
        }else{
                $("#popup-window #login-form").append("<div id='invalid-login'>Invalid Login</div>");
            }

    },"json");

Everything works except

window.location = "http://www.whatever.com/admin";

and the window object isn't what I expect. Looking through it I found that the following does work

window[0].ownerDocument.location.href="http://www.whatever.com/admin";

in everything I tested (IE7, IE8, IE9, chrome, firefox). I'm not sure why that is or whether or not this is the correct way to do it.

I'm relatively new to javascript and the DOM and I'd really like to understand what's happening here.

Upvotes: 1

Views: 830

Answers (1)

Eduardo Quintana
Eduardo Quintana

Reputation: 2388

Use window.location.href = "http://www.whatever.com/admin";

Upvotes: 2

Related Questions