0

While user is in a specific page I should allow them to click on a link. Then a lightbox to be shown and once form inside lightbox has been submitted, the lightbox should be closed and user should be redirected to index page.

I have two javascript function, first one is used to show the lightbox, and second one is to submit lightbox form and fading it. The problem is that I do not know how to redirect user to index page.

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

Any jquery solution would be appreciated as well

J888
  • 1,944
  • 8
  • 42
  • 76
  • [window.location](https://developer.mozilla.org/en-US/docs/Web/API/Window.location) – Rob Oct 10 '13 at 04:26
  • possible duplicate of [How can I make a redirect page in jQuery/JavaScript?](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript) – Rob Oct 10 '13 at 04:26
  • @Rob I have already tried them, neither work – J888 Oct 10 '13 at 04:35

2 Answers2

3

Replace the line

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

with this

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

For jquery based redirection use this:

var url = "http://localhost:9001/index";    
$(location).attr('href',url);
Barun
  • 1,520
  • 2
  • 12
  • 18
  • tested your code; works fine for me if I comment the box and frame elements and put some other valid url. – Barun Oct 10 '13 at 04:51
  • both work, u trying hiting the url in browser ? Also check for javascript errors in console. – Barun Oct 10 '13 at 04:53
  • Both addresses are correct, firebug shows it tries to redirect to localhost:9001/index for almost a second then redirect to the current page. – J888 Oct 10 '13 at 05:09
  • @J888 then probably index page is forward to this page. you must have got what was missing by now, Please share it – Barun Oct 21 '13 at 09:47
0

Try using:

  window.location.href=url
Anuj Aneja
  • 1,323
  • 11
  • 13