-1

I'm trying to create a feature that when a user logs off, they are presented with a confirmation window if they have items in their cart. Below is my code snippet. I'm having trouble with the redirect and reload. I expect it to take me to the localhost:8080 login page, but it stays on whatever page I happen to be on. I know the logoff part is running b/c when I manually refresh the page after the below snippet runs, it changes to a guest user.

EDIT: I looked at the possible duplicate and it made the same suggestion as you guys have so far. Unfortunately it doesn't work. It leaves me on the same page as if nothing happened.

EDIT: It works on IE, but not on chrome and firefox. Updated code snippet

Modified Code Snippet:

$("#menu_sign_out").click(function(){
        var result;
        var httpRequest = new XMLHttpRequest();  //Asynchronous
        httpRequest.onreadystatechange = function() {
            if(this.readyState == 4 && this.status == 200){
                result = this.responseText;
                console.log(result);
                myObj = JSON.parse(this.responseText);
                if(myObj.count > 0){
                    if(confirm("You have items in your cart. Are you sure you want to leave?")){
                        var request = new XMLHttpRequest();  //Synchronous
                        request.open('GET', "/logout", false);
                        request.send(null);
                        window.location.replace('/login');
                        //window.location.href='/login';
                    } else {
                        //do nothing
                    }
                } else {
                    var request = new XMLHttpRequest();  //Synchronous
                    request.open('GET', "/logout", false);
                    request.send(null);
                    window.location.replace('/login');
                    //window.location.href='/login';
                }
            }
        };
        httpRequest.open('GET', "/api/cart/countItems");
        httpRequest.send();
});
Will Estes
  • 63
  • 10
  • Have you tried `location.href = yourURL`? – Lixus Jun 01 '17 at 18:53
  • `location.reload()` reloads the page. Any code after that won't run. Also, just use `location.href = ''` rather than using jQuery. – Heretic Monkey Jun 01 '17 at 18:54
  • to quote [How to redirect to another webpage in JavaScript/jQuery?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage-in-javascript-jquery) "One does not simply redirect using jQuery" – Patrick Barr Jun 01 '17 at 18:55
  • @Lixus - I tried your suggestions and unfortunately no luck. – Will Estes Jun 01 '17 at 19:14
  • @PatrickBarr - See edit, this unfortunately didn't help. – Will Estes Jun 01 '17 at 19:15
  • @David - Can you please unmark as duplicate? I don't believe this question will be solved with that question's answer. – Will Estes Jun 01 '17 at 19:17
  • @WillEstes: Can you elaborate on the problem and how it's different? "It didn't work" doesn't really mean it's not a duplicate. It still looks like you're just asking how to redirect in JavaScript. And the code posted does it very incorrectly. So the linked duplicate shows how to redirect with JavaScript. What is your updated code? When you debug, how does it fail? – David Jun 01 '17 at 19:20
  • @David - I added the new code snippet. I tried both the href and replace. Both produced the same results. It leaves me on the same page as if nothing happened. It takes me to localhost:8080/login when I manually refresh after it runs. – Will Estes Jun 01 '17 at 19:28
  • I tried the manual refresh on other pages (since users can logoff from any page). The manual refresh does not redirect, this was just a fluke of being on a specific page. – Will Estes Jun 01 '17 at 19:39
  • `$("#menu_sign_out").click(function(){` what is `$("#menu_sign_out")` – Kevin B Jun 02 '17 at 19:07
  • This is the ID associated with the logoff. `Sign Out` – Will Estes Jun 02 '17 at 19:11
  • To anyone who was wondering, I ended up turning the whole piece of code into synchronous JQuery, rather than asynchronous to synchronous demonstrated above. – Will Estes Jun 12 '17 at 11:50
  • `$("#menu_sign_out").click(function(event){` `event.preventDefault();` `var $result; ` `$.ajax({` `url:'/api/cart/countItems',` `cache: false,` `success: function(data) {` `if(data.count > 0) {` `if(confirm("You have items in your cart. Are you sure you want to leave?")){` `$result = true;` `} else {` `$result = false;` `}` `} else {` `$result = true;` `}` `},` `async: false //Non-Asynchronous (aka Synchronous) call` `});` `if ($result) {` `window.location.href='/logout';` `}` `});` – Will Estes Jun 12 '17 at 11:50

1 Answers1

-1

Try replacing:

location.reload();
$(location).attr('href','http://localhost:8080/login');

with:

document.location.href='http://localhost:8080/login';
Chris Thornton
  • 162
  • 2
  • 12