1

I need a code that changes the whole url in the address bar, (in javascript). I have already searched upon this question all over the internet and found this code

<script type="text/javascript">
function ChangeUrl(title, url) {
 if (typeof (history.pushState) != "undefined") {
 var obj = { Title: title, Url: url };
 history.pushState(obj, obj.Title, obj.Url);
 } else {
 alert("Browser does not support HTML5.");
 }
}
</script>
<input type="button" value="Page1" onclick="ChangeUrl('Page1', 'Page1.htm');" />
<input type="button" value="Page2" onclick="ChangeUrl('Page2', 'Page2.htm');" />
<input type="button" value="Page3" onclick="ChangeUrl('Page3', 'Page3.htm');" />

But this doesn't changes the whole URL, it just changes

localhost/index.php

to

localhost/Page1.htm

Is there a way possible to change the whole URL? like from

localhost.index.php

to

Page1.htm

asked Dec 30, 2020 at 11:17

2 Answers 2

4

No.

The History API does not let you change the apparent Origin of the page. That would be too useful for people making Phishing attacks.

You can only change the path and what follows it.

answered Dec 30, 2020 at 11:20
Sign up to request clarification or add additional context in comments.

Comments

-1
answered Dec 30, 2020 at 11:19

1 Comment

replaceState is, like the pushState method used in the question, restricted to adjusting the path of the URL. It can't achieve the OP's desired goal.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.