【问题标题】:removing querystring from current page url in javascript从javascript中的当前页面url中删除查询字符串
【发布时间】:2012-10-03 06:14:56
【问题描述】:

我想在按钮单击的 javascript 中从当前页面 url 中删除查询字符串 你能帮我看看上面的代码吗?

【问题讨论】:

    标签: javascript .net


    【解决方案1】:

    试试

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

    【讨论】:

    • @user1619874 这仅适用于支持 pushState 的浏览器,即不支持 ie
    • 你是个天才!!我已经厌倦了人们告诉我这是不可能的,你的解决方案就像一个魅力!这个答案应该被接受!!希望我的支持至少有所帮助!谢谢大佬!!
    【解决方案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
    

    【讨论】:

      【解决方案3】:

      在这里,试试这个:

      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];
      

      【讨论】:

      • 好的,你有我可以看的例子吗?
      • 这是我要修改的网址“localhost:2314/RewardPointsSystem/Admin/…”当我点击按钮时我想用网址“localhost:2314/RewardPointsSystem/Admin/PointCalculations.aspx”加载我的页面
      • 我无法访问该位置,所以这不是我可以查看的示例。
      • 这是本地 URL...您无法访问此...但这是我要修改的页面的示例 url
      • 将 url 从 ...RewardPointsSystem/Admin/PointCalculations.aspx?date=20-Sep-2012 更改为 ...RewardPointsSystem/Admin/PointCalculations.aspx
      【解决方案4】:

      您可以只修剪“?”之后的整个字符串签到

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 2020-04-20
        相关资源
        最近更新 更多