最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How can I make window.location.replace work in IE 9? - Stack Overflow

programmeradmin3浏览0评论

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.");   
    }
},

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.");   
    }
},
Share Improve this question asked Jun 5, 2012 at 11:35 NinjaCatNinjaCat 10.2k9 gold badges46 silver badges65 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 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!

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;
}

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.");   
    }

try

window.location.href = 'newpage.php'

window.location = "newpage.php";

or

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

评论列表(0)

  1. 暂无评论