5

In my code, I'm assigning the following:

window.location.href = "www.example.com/test";

But when the page actually loads, the browser URL is www.example.com/test/www.example.com/test. I'm not appending anything to the URL, and I'm not sure how its appending the URL again.

3 Answers 3

8

I think you're missing the "http" or "https" part. Have you tried the following?

window.location.href = "https://www.example.com/test";

or

window.location.href = "http://www.example.com/test";
Sign up to request clarification or add additional context in comments.

Comments

4

Because you forgot the protocol. If you omit the protocol, window.location.href thinks you are trying to access a folder with the name of www.example.com, relative to the page you are currently on.

window.location.href="http://www.example.com/test/" will ensure that you access the external website www.example.com.

Hope this helps! :)

Comments

0

Check the way you are constructing the url, sometimes we miss the host, or enter the incorrect path

A safe way to change the URl is by making changes in the exisiting URL first get the existing URL by

let exisitingURl = window.location.href;

now manipulate this url, for eg

exisitingURL = exisitingURL.replace('/auth', '/gateway');

now go to the url by

window.location.href = existingURL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.