【发布时间】:2012-10-03 06:14:56
【问题描述】:
我想在按钮单击的 javascript 中从当前页面 url 中删除查询字符串 你能帮我看看上面的代码吗?
【问题讨论】:
标签: javascript .net
我想在按钮单击的 javascript 中从当前页面 url 中删除查询字符串 你能帮我看看上面的代码吗?
【问题讨论】:
标签: javascript .net
试试
var query = window.location.href.match(/^(.*)\?/);
if (query){
history.pushState(null, "", query[1]);
}
【讨论】:
您不能在不重定向或重新加载页面的情况下简单地删除查询字符串。因此,您将使用 windows.location 更改位置或重定向。 getPathFromUrl() 函数从给定的 URL 中删除查询字符串。
这是你怎么做的例子:
function getPathFromUrl(url) {
return url.split("?")[0];
}
var testurl='http://localhost:2314/RewardPointsSystem/Admin/PointCalculations.aspx?date=20-Sep-2012%22'
window.location=getPathFromUrl(testurl); // loads http://localhost:2314/RewardPointsSystem/Admin/PointCalculations.aspx
【讨论】:
在这里,试试这个:
window.location = String(window.location).match(/(.*?)\?/)[1];
或者使用 cmets 中的示例 URL
window.location = "http://localhost:2314/RewardPointsSystem/Admin/PointCalculations.aspx?date=20-Sep-2012%22".match(/(.*?)\?/)[1];
【讨论】:
您可以只修剪“?”之后的整个字符串签到
【讨论】: