rapidfire69
rapidfire69

Reputation: 103

Change the whole url in the address bar (Javascript)

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

Upvotes: 1

Views: 2051

Answers (2)

Quentin
Quentin

Reputation: 944451

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.

Upvotes: 4

John V
John V

Reputation: 875

Use History.replaceState for your problem

https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState

Upvotes: -1

Related Questions