Search code examples
javascripttypescriptnavigationwindow

How do I change the browsers location in javascript?


I'm writing a legal consent page which works in a similar way to a sign in page, in that it is totally separate from the main website. The main website sends users to the page with a query parameter of where it would like them to return to.

The main site has a url like this:

portal.myCloudProject.company.cloud

The URL I'm accessing the consent page looks like this:

consent.differentCloudProject.company.cloud?origURL="portal.myCloudProject.company.cloud"

When the user consents by clicking a button, I want the browser to navigate to origURL. My code looks like this:

var searchParams = new URLSearchParams(window.location.search);
var returnURL = searchParams.get('origURL') as string;
console.log(returnURL);
window.location.replace(returnURL);

However when I click the consent button, my browser navigates to:

consent.differentCloudProject.company.cloud/portal.myCloudProject.company.cloud

I've also tried to set the location like this:

window.location.assign(returnURL);

and

window.location.href = returnURL;

What am I doing wrong?


Solution

  • Use window.location.replace("//" + returnURL);