codematix
codematix

Reputation: 1337

Setting location.href removes port number

I am developing an application whose initial page resides at:

https://localhost:8443/#/signin

I want to perform a redirect from JavaScript to the following location:

https://localhost:8443/admin

To do this, I write code as follows:

location.href = location.origin + '/admin';

However, when I do perform this, the page browses to:

https://localhost/admin

I also tried location.assign() with the same result.

Why does setting location.href or location.assign() remove the port-number from the URL?

Any other way I can accomplish this?

Upvotes: 0

Views: 2772

Answers (1)

Cerbrus
Cerbrus

Reputation: 72907

As @RyanNghiem said, add location.port:

location.href = location.origin + ':' + location.port + '/admin';

Maybe it's a good idea to check if location.port exists, to prevent the colon from being added without a port:

location.href = location.origin + (location.port ? ':' + location.port : '') + '/admin';

Upvotes: 3

Related Questions