在javascript中从当前页面网址中删除查询字符串

时间:2012-10-03 06:14:57

标签: javascript .net

我想在javascript按钮Click上从当前页面url中删除查询字符串 你能用上面的代码帮助我。

4 个答案:

答案 0 :(得分:2)

var query = window.location.href.match(/^(.*)\?/);
if (query){
    history.pushState(null, "", query[1]);
}

答案 1 :(得分:2)

如果没有重定向或重新加载页面,您无法简单地删除查询字符串。因此,您将使用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

答案 2 :(得分:1)

在这里,试试这个:

window.location = String(window.location).match(/(.*?)\?/)[1];

或在评论中使用示例网址

window.location = "http://localhost:2314/RewardPointsSystem/Admin/PointCalculations.aspx?date=20-Sep-2012%22".match(/(.*?)\?/)[1];

答案 3 :(得分:0)

你可以修剪整个字符串继续“?”登录