3

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.");   
    }
},
NinjaCat
  • 9,974
  • 9
  • 44
  • 64

6 Answers6

12

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!

Jimbo Jonny
  • 3,549
  • 1
  • 19
  • 23
  • 1
    interesting... it was the console.log... Will open up a new question as to the difference between .location.href and .location.replace... – NinjaCat Jun 05 '12 at 12:15
5

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;
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
vie
  • 51
  • 1
  • 1
  • +1 right, see [this post](http://stackoverflow.com/questions/8809025/window-location-not-working-in-ie?lq=1) for the reason it works. – Adriano Repetti Oct 10 '12 at 15:11
3

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.");   
    }
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • 1
    I disagree. The big difference between location.href and location.replace is browser history. location.replace will take the page out of a users history, location.href will not. For more, see [link](http://stackoverflow.com/questions/1388384/stop-page-from-appearing-in-browser-history) and [link](http://stackoverflow.com/questions/15193359/does-android-support-window-location-replace-or-any-equivalent) – Tim Mickey Aug 06 '15 at 19:02
1

try

window.location.href = 'newpage.php'

Muhammad Ummar
  • 3,541
  • 6
  • 40
  • 71
1
window.location = "newpage.php";

or

window.location.href = "newpage.php";
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
Shehzad Bilal
  • 2,535
  • 2
  • 18
  • 27
1
if(html) {
  window.location.href = "filename.php";
} else {
  $("#add_err").html("Wrong username or password.");
}
Adam Bremen
  • 685
  • 1
  • 7
  • 18
  • location.href is a property, not a function. This will fail. – Jimbo Jonny Jun 05 '12 at 12:25
  • -1 retracted after your change, it's correct now :) But no +1 since changing location.href isn't the same as location.replace, and there's no need to remove the location.replace from the OP's answer :( – Jimbo Jonny Oct 17 '12 at 16:21