0

I am trying to replace the url and load the page.

Example:

URL: http://www.example.com/account/edit

I want to replace this as http://www.example.com/account/add. And load this page.

For that, I tried by using location.pathname.

location.pathname = "/account/add";

When using like above, the add page is displayed. But after that, URL http://www.example.com/account/add is loading. Then the add page is not displayed. I get that page is not found message.

The host name should vary based on running the project. So I want to resolve this without based on host name.

How can I achieve it?

3 Answers 3

1

If you want to retain the page in your session history (meaning you want to be able to use the back button to it) use the assign method. Otherwise, you replace.

Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

http://mdn.beonex.com/en/DOM/window.location.html

Sign up to request clarification or add additional context in comments.

Comments

0
window.location.href = "http://www.example.com/account/add";
OR
window.location.replace("http://www.example.com/account/add");

This will redirect to the new page.

2 Comments

The host name "www.example.com" should vary based on the project. So Cannot able to replace directly.
window.location.href(or replace) = '/account/add/'; should work fine too
0

Actually you can change your url some other ways like below:

window.location.assign("http://www.mozilla.org"); // or
window.location = "http://www.mozilla.org";

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.