Tech Guy
Tech Guy

Reputation: 447

How to redirect to multiple url's using from javascript?

I have a requirement to clear the cookies and to redirect to a new url after clicking on a link. I wrote the below code which calls "logOffRedirectUrlForCookieDeletion" which is responsible to clear the cookies and redirect the application to the homepage and "myUrlAfterCookirDeletion" is the new url that needs to be redirected from the homepage after cookie deletion. If I use any one of the "window.location.href" statements, my code works absolutely fine but if I use two "window.location.href" statements only the second one is getting executed. Can someone please suggest me how to use 2 url redirection using window.location.href

ClearCookiesAndRedirect:function(redirectURL){
         if (confirm("Do you want to leave the website?") == true) {  
                window.location.href = logOffRedirectUrlForCookieDeletion;
                window.location.href = "myUrlAfterCookirDeletion";

            } else {
               return;
            }   
    },

Upvotes: 2

Views: 2668

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Add an iframe to delete cookies. That would load the cookie deletion page + not redirect, after that redirect:

iframe=document.createElement("iframe");
iframe.src="cookiedeletion.html";
iframe.onload=function(){
 //cookies deleted lets redirect
window.location.href="newurl";
};
document.body.appendChild(iframe);

However, why not delete the cookies right on the page? Much easier...

Upvotes: 1

Related Questions