如何删除 URL 中的参数并将其显示在地址栏中而不导致 JavaScript 中的重定向?
我找到了许多关于如何提取不带参数的 URL 的答案。
如何重写地址栏中的 URL,而不导致页面使用新 URL 重新加载?
shortURL = top.location.href.substring(0, top.location.href.indexOf('?'));
top.location.href = shortURL // Causes redirect
目标是提取 Javascript 中的参数,但不将它们显示在地址栏中。
I have found numerous answers on how to extract the URL without the parameters.
How do you rewrite the URL in the address bar without causing the page to reload with the new URL?
shortURL = top.location.href.substring(0, top.location.href.indexOf('?'));
top.location.href = shortURL // Causes redirect
The goal is to extract the parameters in Javascript but not display them in the address bar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在支持 History 对象的现代浏览器中,您可以使用
history.replaceState()
或history.pushState()
更改当前 URL,而不更改当前页面。您可以更改的内容存在限制(例如,出于安全原因,您不能以这种方式更改域/来源)。有关这些方法的摘要,请参阅此处。
浏览器历史记录是您在浏览会话中所处位置的记录。
.replaceState()
允许您将历史列表中的当前项目替换为其他项目。.pushState()
在浏览器历史记录中添加一个新项目,并且都可以更改浏览器 URL 栏中显示的 URL,而无需重新加载页面。您可以根据您希望浏览器的“后退”按钮对该特定页面条目的行为方式来选择使用哪种方法。注意:IE 10 及更高版本支持这些 API。
在不支持历史记录 API 的旧浏览器版本中,无需重新加载页面即可更改 URL 的唯一部分是 URL 末尾的哈希标记(
#
符号后面的部分)。In modern browsers with support for the History object, you can use either
history.replaceState()
orhistory.pushState()
to change the current URL without changing the current page. There are limitations on what you can change (for example, you cannot change the domain/origin this way for security reasons).See here for a summary of these methods.
The browser history is a recording of where you have been in your browsing session.
.replaceState()
allows you to replace the current item in the history list with a different one..pushState()
adds a new item to the browser history and both change the URL displayed in the browser URL bar without reloading the page. You select which method to use depending upon how you want the browser's "back" button to behave for this particular page entry.Note: These APIs are supported in IE 10 and later.
In older browser versions without support for the history API, the only part of the URL you can change without reloading the page is the hash tag (the part after a
#
symbol) at the end of the URL.在html5中,可以在不重新加载页面的情况下重写url(出于安全原因仍然不能更改域名),使用history api您可以编写:
其中前两个参数在某种程度上是任意的,第三个参数是新的url (不包括域名)。如果您想启用后退按钮以返回上一个网址,请使用
pushState
而不是上面的replaceState
。在这篇博客文章。关于浏览器支持,最近的 Firefox 和 Chrome 版本都支持,但仅在 IE10+ 中支持,目前还不是很常见。有关详细信息,请参阅兼容性矩阵或浏览器实现详细信息。
In html5, rewriting url without reloading the page is possible (still you can not change the domain name for security reasons), using history api you can write:
in which the first two parameters are somehow arbitrary, and the third parameter is the new url (not including domain name). If you want to enable back button for returning to previous url, use
pushState
instead ofreplaceState
above. Read more about these functions in this blog post.Regarding browser support, it is supported in recent Firefox and Chrome versions, but only in IE10+, which is not very common yet. For details see compatibility matrix or browser implementation details.
如果不进行重定向,则无法更改地址栏中的值。这将是网络钓鱼诈骗者的梦想成真!
不过,您可以更改片段标识符:(在 JavaScript 中,
window.location .hash
值)但是更改查询字符串将重定向页面。
You can't change the value in the address bar without redirecting. That would be a phishing scammer's dream come true!
You can, however, change the fragment identifier: (in JavaScript, the
window.location.hash
value)But changing the query string will redirect the page.
您也许可以使用 HTML 5 历史记录 API 中的新推送状态,它允许您更改 URL,而无需实际重新加载浏览器。
检查 http://badassjs.com/post/ 840846392/location-hash-is-dead-long-live-html5-pushstate 作为一个快速示例,http://diveintohtml5.info/history.html 了解更深入的示例和限制:
You might be able to use the new pushstate that is part of the HTML 5 history API, which allows you to change the URL without actually reloading the browser.
Check http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate for a quick example, and http://diveintohtml5.info/history.html for more in depth examples and limitations:
我不认为有这种可能性,我的意思是你可能可以在加载后重写 URL 并添加 return false,这样你就可以阻止重新加载,但否则你必须在 url 上执行表单 POST 才能实现这一点没有显示参数。
I don't think that there is a possibility for that, I mean you probably could rewrite the URL after its loaded and add return false, so you prevent the reload but otherwise you would have to do a form POST on the url to achieve that no parameter is shown.