Javascript 如何在页面加载后从 url 中删除查询字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10700937/
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
How can I remove the query string from the url after the page loads?
提问by Sangam254
I have a URL with a long query string attached to it. After the page loads, I do not require the query string. So I want to remove the query string from the address bar without a page reload.
我有一个带有长查询字符串的 URL。页面加载后,我不需要查询字符串。所以我想在不重新加载页面的情况下从地址栏中删除查询字符串。
I tried parent.location.hash = '';
and window.location.href = '/#'
我试着parent.location.hash = '';
和window.location.href = '/#'
They did not make a difference.
他们没有任何区别。
采纳答案by mattytommo
You can't do that without reloading the page, imagine if you could put whatever you wanted in the browser address bar? Security fun :)
如果不重新加载页面,您就无法做到这一点,想象一下您是否可以在浏览器地址栏中放置任何您想要的内容?安全乐趣:)
Althoughyou can now do it in HTML5 (which will only work on browsers supporting it) using the new history API, but realistically, your scenario best warrants a rewrite instead of including that (seems sledgehammer to crack a nut).
尽管您现在可以使用新的历史 API在 HTML5(仅适用于支持它的浏览器)中完成它,但实际上,您的场景最好保证重写而不是包含它(似乎是大锤破解坚果)。
As you said you don't need the query string after the page loads, you should reallypost back, then redirected to another URL after you've finished your processing.
正如您所说,您在页面加载后不需要查询字符串,您应该真正回发,然后在完成处理后重定向到另一个 URL。
回答by germankiwi
As others have said, you can do this using the History APIin modern browsers (IE10+, FF4+, Chrome5+). There was no full example in the answers, so figured I'd share my solution, as I just had a requirement to do the same thing:
正如其他人所说,您可以在现代浏览器(IE10+、FF4+、Chrome5+)中使用History API来做到这一点。答案中没有完整的例子,所以我想我会分享我的解决方案,因为我只需要做同样的事情:
history.pushState(null, "", location.href.split("?")[0]);
If you are using Modernizr, you can also check if the History API is available like so:
如果您使用的是Modernizr,您还可以检查 History API 是否可用,如下所示:
if (Modernizr.history) {
history.pushState(null, "", location.href.split("?")[0]);
}
回答by Quentin
That is achievable in modern browsersusing the history api, but probably isn't the best solution to your problem.
这在使用history api 的现代浏览器中是可以实现的,但可能不是您问题的最佳解决方案。
history.replaceState({}, 'some title', '/');
It sounds like you would be better off processing the data and then redirecting to the homepage instead of returning an HTML document directly.
听起来您最好处理数据然后重定向到主页,而不是直接返回 HTML 文档。
Since you aren't wanting to keep the URL around, it won't be useful for bookmarking, so it is quite likely you would be better off making a POST request.
由于您不想保留 URL,因此它对书签没有用,因此很可能您最好发出 POST 请求。
This suggests that you should be using the POST-Redirect-GET pattern.
这表明您应该使用POST-Redirect-GET 模式。
回答by Fezal halai
Remove query string value from address bar
从地址栏中删除查询字符串值
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
回答by d4rkpr1nc3
Use history.replaceState({}, "Title", "page.html");
用 history.replaceState({}, "Title", "page.html");
same syntax for pushState
. However you will have to find a way to make IE understand that.
相同的语法pushState
。但是,您必须找到一种方法让 IE 理解这一点。
A better way would be to use Apache mod_rewrite
module. It's quite easy to use.
更好的方法是使用 Apachemod_rewrite
模块。它很容易使用。
回答by ChristianG
I use this code snippit in my personal projects where i need to remove URL params without re-loading:
我在我的个人项目中使用此代码片段,我需要在不重新加载的情况下删除 URL 参数:
var newURL = location.href.split("?")[0];
window.history.pushState('object', document.title, newURL);
回答by curiousBoy
I want to add one more way to do this, especially for the ones who are using $routeProvider
.
我想再添加一种方法来做到这一点,特别是对于那些使用$routeProvider
.
As it has been mentioned in some other answers, you do this by using:
正如其他一些答案中提到的那样,您可以使用以下方法执行此操作:
var yourCurrentUrl = window.location.href.split('?')[0];
window.history.replaceState({}, '', yourCurrentUrl );
or by creating a new record in history stack:
或者通过在历史堆栈中创建一个新记录:
window.history.pushState({}, '', yourCurrentUrl );
For a very detailed and nice explanation about doing with history.pushState
and history.replaceState
, you can read this post on SO.
有关如何使用history.pushState
and的非常详细和漂亮的解释history.replaceState
,您可以阅读SO 上的这篇文章。
HOWEVER if you are using Angular$routeProvider
in your app, then it will still capture this change if that matches with any existing pattern. To avoid that, make sure your $routeProvider
has reloadOnSearchflag set to false
because by default it is true
.
但是,如果您$routeProvider
在应用程序中使用 Angular,那么如果它与任何现有模式匹配,它仍然会捕获此更改。为避免这种情况,请确保您$routeProvider
已将reloadOnSearch标志设置为,false
因为默认情况下它是true
.
For example:
例如:
$routeProvider.when("/path/to/my/route",{
controller: 'MyController',
templateUrl: '/path/to/template.html',
//Secret Sauce
reloadOnSearch: false//Make sure this is explicitly set to false
});
回答by Code Spy
Updated Code Now it will work
更新代码现在可以使用了
Just Add Below code in you page whose link you want to change.
只需在您要更改其链接的页面中添加以下代码。
// javascript function
function buildLin(first) {
var firs = first.toString()
//alert(firs);
//document.getElementById("team").value = "1";
document.location.hash = "#" + firs.replace(/ /g, "_");
//alert(document.location.hash);
}
//jQuery to call above function after page loads
$(document).ready(function () {
buildLin(2);
});
Don't forget to add http://code.jquery.com/jquery-latest.json your page ?
不要忘记 在您的页面上添加 http://code.jquery.com/jquery-latest.js?
回答by Recev Yildiz
I have faced the same problem. My solution was these: Get the request like this:
我遇到了同样的问题。我的解决方案是:获取这样的请求:
operation.aspx?opr=lock
After finishing the operation redirect like this:
完成操作后像这样重定向:
Response.Redirect("operation.aspx");
In the response, if there is any message, I print them like this:
在响应中,如果有任何消息,我会像这样打印它们:
if (Session["InfoMessages"] != null)
{
lblMessage.Text = (string)Session["InfoMessages"];
Session["InfoMessages"] = null;
}
I hope it will help others :)
我希望它会帮助其他人:)
回答by Widor
You could avoid a query string altogether by using POST
instead of GET
.
您可以通过使用POST
代替 来完全避免查询字符串GET
。