Javascript 如何在不离开页面的情况下更改浏览器中显示的URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4089178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:33:35  来源:igfitidea点击:

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

javascripturlbrowser-history

提问by mjr

Is it possible with JavaScript to change the browser's URL, but not leave the page?

是否可以使用 JavaScript 更改浏览器的 URL,但不能离开页面?

回答by Adam

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

在较旧的浏览器中,您不能在不离开页面的情况下更改地址栏中的 url。但是您可以在不离开页面的情况下更改 url 的哈希部分。也就是说你可以改变www.example.com,以www.example.com#new_text用JavaScript window.location.hash = "new_text"; 之后的一切#都可以改变。

However, in HTML5 there is a new History API which allows you to change the part of the URL after the domain. So you still cannot change www.example.comto www.BankOfAmerica.com(for security reasons), but you can change www.example.com/footo www.example.com/bar.

但是,在 HTML5 中有一个新的 History API,它允许您更改域之后的 URL 部分。所以您仍然不能更改www.example.comwww.BankOfAmerica.com(出于安全原因),但您可以更改www.example.com/foowww.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 pushStatemethod.

检查何时可以使用...以查看哪些浏览器支持 HTML5 会话历史记录管理并支持新pushState方法。

In addition there is a JavaScript library which will normalizes the history API across browsers and changes the URL in new browsers and uses the hash portion for old browsers. See history.js.

此外,还有一个 JavaScript 库,它将跨浏览器规范化历史 API,并在新浏览器中更改 URL,并为旧浏览器使用哈希部分。参见history.js

回答by mikerobi

I'm going to assume you are talking about the visible URL in the URL bar.

我假设您正在谈论 URL 栏中的可见 URL。

The answer is No, it is a major security vulnerability when an application tries trick users into thinking that are at another site.

答案是否定的,当应用程序试图诱使用户认为它在另一个站点时,这是一个主要的安全漏洞。

回答by kijin

You can change anything after the hash mark (#) as this is frequently used in Ajax applications such as Google search and the new Twitter. (That's why everything appears after the hash mark in those apps.) But if you change anything else, the page will have to be reloaded.

您可以更改井号 ( #)后的任何内容,因为它经常用于 Ajax 应用程序,例如 Google 搜索和新的 Twitter。(这就是为什么所有内容都出现在这些应用程序中的井号之后。)但是如果您更改其他任何内容,则必须重新加载页面。

回答by Denilson Sá Maia

No, it's not possible. And, whenever it is possible, that is a browser bug (I know about previous security bugs related to this behavior, and they were fixed in past).

不,这不可能。而且,只要有可能,那就是浏览器错误(我知道以前与此行为相关的安全错误,并且它们过去已修复)。

Actually... You can change the last portion of the URL, anything after the #character. But the hostname and path can't be changed without leaving the page.

实际上...您可以更改 URL 的最后一部分,#字符之后的任何内容。但是不离开页面就不能更改主机名和路径。