How to change the URL displayed in the browser without leaving the page

Is it possible for JavaScript to change the browser url but not leave the page?

+8
javascript url browser-history
Nov 03 '10 at 16:20
source share
4 answers

In older browsers, you can not change the URL in the address bar without leaving the page. But you can change the hash of the URL without leaving the page. That is, you can change www.example.com to www.example.com#new_text using JavaScript window.location.hash = "new_text" ; everything after # can be changed.

However, HTML5 has a new history API that allows you to change the portion of the URL after the domain. Thus, you still cannot change www.example.com to www.BankOfAmerica.com (for security reasons), but you can change www.example.com/foo to www.example.com/bar .

 history.pushState("object or string representing the state of the page", "new title", "newURL"); 

Check When can I use ... to see which browsers support HTML5 session history management and support the new pushState method.

In addition, there is a JavaScript library that normalizes the history API in browsers and changes the URL in new browsers and uses the hash part for older browsers. See history.js .

+13
Nov 03 '10 at 16:25
source share

I am going to assume that you are talking about the visible URL in the URL string.

The answer "No" is a serious security vulnerability when an application tries to trick users of thinking who are on another site.

+5
Nov 03 '10 at 16:24
source share

You can change anything after the hash mark ( # ), as it is often used in Ajax applications such as Google search and new Twitter. (That's why everything appears after the hash mark in these applications.) But if you change something, the page should be reloaded.

0
Nov 03 '10 at 16:23
source share

No, It is Immpossible. And, when possible, this is a browser error (I know of previous security errors associated with this behavior, and they have been fixed in the past).

Actually ... You can change the last part of the URL, anything after the # character. But the host name and path cannot be changed without leaving the page.

-one
Nov 03 '10 at 16:25
source share



All Articles