我有一个连接到它长的查询字符串的URL。 页面加载后,我不要求查询字符串。 所以,我想删除从地址栏查询字符串,而不重新加载页面。
我试图parent.location.hash = '';
和window.location.href = '/#'
他们没有有所作为。
我有一个连接到它长的查询字符串的URL。 页面加载后,我不要求查询字符串。 所以,我想删除从地址栏查询字符串,而不重新加载页面。
我试图parent.location.hash = '';
和window.location.href = '/#'
他们没有有所作为。
你不能这样做,无需重新加载页面,想象一下,如果你可以把你在浏览器地址栏中想什么? 安全乐趣:)
虽然你现在可以做到这一点在HTML5中使用新的(这只能在浏览器支持它的工作) 的历史API ,但实际上,你的情况最好的权证重写,而不是包括(好像大锤牛刀)。
至于你说你并不需要在页面加载后的查询字符串,你真的应该回来后,然后重定向到另一个网址你完成你的处理之后。
正如其他人所说,你可以用做历史API在现代浏览器(IE10 +,FF4 +,Chrome5 +)。 有没有在答案没有完整例子,所以想我会分享我的解决办法,因为我刚刚有了一个要求做同样的事情:
history.pushState(null, "", location.href.split("?")[0]);
如果你正在使用Modernizr的 ,你也可以检查历史API可以像这样:
if (Modernizr.history) {
history.pushState(null, "", location.href.split("?")[0]);
}
这是可以实现的现代浏览器使用的历史API ,但可能不是你的问题的最佳解决方案。
history.replaceState({}, 'some title', '/');
这听起来像是你将处理过的数据,然后重定向到主页,而不是直接返回一个HTML文档更好。
既然你不想保持网址周围,它不会为书签有用的,所以它很可能你会推迟作出POST请求更好。
这表明,你应该使用POST重定向消息GET模式 。
使用history.replaceState({}, "Title", "page.html");
同样的语法pushState
。 然而,你必须找到一种方法,使IE明白。
更好的办法是使用Apache mod_rewrite
模块。 这是很容易使用。
更新后的代码现在将工作
只需添加以下代码要更改其链接你的页面。
// 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);
});
不要忘记添加http://code.jquery.com/jquery-latest.js您的网页上
我所面临的同样的问题。 我的解决办法是这些:得到这样的要求:
operation.aspx?opr=lock
完成操作后重定向这样的:
Response.Redirect("operation.aspx");
在响应中,如果有任何消息,我打印出来是这样的:
if (Session["InfoMessages"] != null)
{
lblMessage.Text = (string)Session["InfoMessages"];
Session["InfoMessages"] = null;
}
我希望这会帮助别人:)
我用在我需要删除网址参数,而无需重新加载我的个人项目,这个代码片段:
var newURL = location.href.split("?")[0];
window.history.pushState('object', document.title, newURL);
我想补充一个办法做到这一点,尤其是对谁正在使用的那些$routeProvider
。
因为它在一些其他的答案被提及,通过使用这样做:
var yourCurrentUrl = window.location.href.split('?')[0];
window.history.replaceState({}, '', yourCurrentUrl );
或者通过创造了历史堆栈的新纪录:
window.history.pushState({}, '', yourCurrentUrl );
有关使用做了非常详细的和好的解释history.pushState
和history.replaceState
,你可以阅读关于所以这个职位 。
然而,如果你采用了棱角分明 $routeProvider
在你的应用程序,那么它仍会捕捉这种变化如果与任何现有的模式匹配。 为了避免这种情况,请确保您的$routeProvider
已经reloadOnSearch标志设置为false
,因为默认情况下它是true
。
例如:
$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
});
从地址栏中删除查询字符串值
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
你可以通过使用完全避免查询字符串POST
,而不是GET
。