代码之家  ›  专栏  ›  技术社区  ›  Mathias F

从URL中删除querystring

  •  120
  • Mathias F  · 技术社区  · 14 年前

    在javascript中,从路径中删除querystring的简单方法是什么? 我看到了一个jquery插件,它使用window.location.search。我不能这样做:在我的例子中,URL是从Ajax设置的一个变量。

    var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3&SortOrder=dsc'
    
    8 回复  |  直到 7 年前
        1
  •  302
  •   Robusto    8 年前

    一个简单的方法是:

    function getPathFromUrl(url) {
      return url.split("?")[0];
    }
    

    对于那些也希望 移除哈希 (不是原始问题的一部分) 当不存在查询字符串时 ,这需要更多一点:

    function stripQueryStringAndHashFromPath(url) {
      return url.split("?")[0].split("#")[0];
    }
    

    编辑

    @caub(最初是@crl)建议使用一个更简单的组合,既适用于查询字符串,也适用于哈希(尽管它使用regexp,以防任何人对此有问题):

    function getPathFromUrl(url) {
      return url.split(/[?#]/)[0];
    }
    
        2
  •  32
  •   Community Nick Dandoulakis    7 年前

    第二更新: 为了提供一个全面的答案,我正在对各种答案中提出的三种方法进行基准测试。

    var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3';
    var i;
    
    // Testing the substring method
    i = 0;
    console.time('10k substring');
    while (i < 10000) {
        testURL.substring(0, testURL.indexOf('?'));
        i++;
    }
    console.timeEnd('10k substring');
    
    // Testing the split method
    i = 0;
    console.time('10k split');
    while (i < 10000) {
        testURL.split('?')[0]; 
        i++;
    }
    console.timeEnd('10k split');
    
    // Testing the RegEx method
    i = 0;
    var re = new RegExp("[^?]+");
    console.time('10k regex');
    while (i < 10000) {
        testURL.match(re)[0]; 
        i++;
    }
    console.timeEnd('10k regex');
    

    Mac OS X 10.6.2上的Firefox 3.5.8的结果:

    10k substring:  16ms
    10k split:      25ms
    10k regex:      44ms
    

    Mac OS X 10.6.2上Chrome 5.0.307.11的结果:

    10k substring:  14ms
    10k split:      20ms
    10k regex:      15ms
    

    请注意,substring方法的功能较差,因为如果URL不包含querystring,它将返回一个空字符串。其他两个方法将按预期返回完整的URL。然而,值得注意的是,substring方法是最快的,特别是在firefox中。


    第一更新: 实际上是split()方法 suggested by Robusto 是我之前建议的更好的解决方案,因为即使没有querystring,它也能工作:

    var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3';
    testURL.split('?')[0];    // Returns: "/Products/List"
    
    var testURL2 = '/Products/List';
    testURL2.split('?')[0];    // Returns: "/Products/List"
    

    原始答案:

    var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3';
    testURL.substring(0, testURL.indexOf('?'));    // Returns: "/Products/List"
    
        3
  •  3
  •   karthik m    12 年前

    一个简单的方法是你可以做如下

    public static String stripQueryStringAndHashFromPath(String uri) {
     return uri.replaceAll(("(\\?.*|\\#.*)"), "");
    }
    
        4
  •  2
  •   plodder    14 年前

    如果你喜欢雷杰克斯……

    var newURL = testURL.match(new RegExp("[^?]+"))
    
        5
  •  2
  •   yckart Matthew Crumley    11 年前
    var path = "path/to/myfile.png?foo=bar#hash";
    
    console.log(
        path.replace(/(\?.*)|(#.*)/g, "")
    );
    
        6
  •  1
  •   damitj07    7 年前

    这可能是一个旧问题,但我已尝试使用此方法删除查询参数。对于我来说,似乎工作得很顺利,因为我需要重新加载以及删除查询参数。

    window.location.href = window.location.origin + window.location.pathname;
    

    另外,由于我使用的是简单的字符串加法操作,所以我猜测性能会很好。但仍值得与 this answer

        7
  •  -1
  •   Boris Guéry    14 年前

    如果需要对URL执行复杂的操作,可以查看 jQuery url parser plugin .

        8
  •  -1
  •   vikyd    8 年前

    如果使用主干.js(其中包含 url anchor 作为路径) query string 可能出现:

    1. 之前 网址锚 :

      var url = 'http://example.com?a=1&b=3#routepath/subpath';
      
    2. 之后 网址锚 :

      var url = 'http://example.com#routepath/subpath?a=1&b=3';
      

    解决方案:

    window.location.href.replace(window.location.search, '');
    // run as: 'http://example.com#routepath/subpath?a=1&b=3'.replace('?a=1&b=3', '');