NinjaCat
NinjaCat

Reputation: 10204

How can I make window.location.replace work in IE 9?

I've got the following code that checks for a username and password, and then redirects to a new page if it's successful. It works on Firefox just fine, but when I try it on IE 9, it just sits and hangs. What do I need to do to make this work on IE?

username=$("#user_name").val();
password=$("#password").val();
$.ajax({
    type: "POST",
    url: "login.php",
    data: "name="+username+"&pwd="+password,
    success: function(html){
    console.log(html);
    if(html=='true') {
        window.location.replace("newpage.php");
    } else {
        $("#add_err").html("Wrong username or password.");   
    }
},

Upvotes: 3

Views: 6372

Answers (6)

vie
vie

Reputation: 51

Well I've been a while looking how to change window.location on IE and this works for me:

var targetlocation = "mylocation";

setTimeout(changeURL,0)

function changeURL()
{
    window.location = targetlocation;
}

Upvotes: 5

Adam Bremen
Adam Bremen

Reputation: 695

if(html) {
  window.location.href = "filename.php";
} else {
  $("#add_err").html("Wrong username or password.");
}

Upvotes: 1

Jimbo Jonny
Jimbo Jonny

Reputation: 3579

First in regard to the other answers: location.replace is not the same as just changing the location.href, it reacts with the history differently, and in many cases location.replace is better for the user experience, so lets not throw the towel in on location.replace quite so quickly!

So I just tested location.replace in IE9 on my machine and it worked.

Then I tested console.log on my IE9 and it choked (that apparently only works if a certain developer panel or tab or something is open I'm reading, though I don't have much experience with IE9's dev tools to say that for sure).

I'm betting your problem is not the location.replace function, but in fact the console.log function that is right before it!

Upvotes: 12

Shehzad Bilal
Shehzad Bilal

Reputation: 2523

window.location = "newpage.php";

or

window.location.href = "newpage.php";

Upvotes: 1

Muhammad Ummar
Muhammad Ummar

Reputation: 3631

try

window.location.href = 'newpage.php'

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

You don't need replae.

if(html=='true') {
        window.location = "newpage.php";
       // or
       window.location.href = "newpage.php";
    } else {
        $("#add_err").html("Wrong username or password.");   
    }

Upvotes: 3

Related Questions