如何重定向到新页面?

时间:2013-10-10 04:17:57

标签: javascript jquery ajax

当用户在特定页面中时,我应该允许他们点击链接。然后提交了一个灯箱,一旦在灯箱内形成,灯箱应该关闭,用户应该被重定向到索引页面。

我有两个javascript函数,第一个用于显示灯箱,第二个用于提交灯箱形式并淡化它。问题是我不知道如何将用户重定向到索引页面。

function rateItem(){
    document.getElementById("box").style.display = "Block";
    document.getElementById("frame").style.display = "Block";
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            document.getElementById("box").innerHTML=xmlhttp.responseText;
        } 
    }
    xmlhttp.open("get","../Item/rate",false);
    xmlhttp.send();
    return false;
}

function rate(id){
    rate = $('#rate').val();
    document.getElementById("box").style.display = "Block";
    document.getElementById("frame").style.display = "Block";
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {

            document.getElementById("box").style.display = "none";
            document.getElementById("frame").style.display = "none";
            window.location="http://localhost:9001/index";   << does not work
            window.location.replace("http://localhost:9001/index"); <<does not work

        } 
    }
    xmlhttp.open("POST","../Item/submit?rate="+rate+"&id="+id,false);
    xmlhttp.send();
}

任何jquery解决方案也将受到赞赏

2 个答案:

答案 0 :(得分:3)

替换

window.location="http://localhost:9001/index"; 

用这个

window.location.replace("http://localhost:9001/index");

对于基于jquery的重定向,请使用:

var url = "http://localhost:9001/index";    
$(location).attr('href',url);

答案 1 :(得分:0)

尝试使用:

  window.location.href=url