2010-03-29 155 views
110

什么是从Javascript中的路径中删除查询字符串的简单方法? 我看到了一个使用window.location.search的Jquery插件。我不能那样做:我的例子中的URL是一个从AJAX设置的变量。从URL中删除查询字符串

var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3&SortOrder=dsc' 
+0

可能的重复[如何从JavaScript中删除哈希window.location(URL)没有页面刷新?](https://stackoverflow.com/questions/1397329/how-to - 删除哈希从窗口位置网址与JavaScript的无页面r) – PayteR 2017-09-10 12:54:44

回答

272

一个简单的方法来获得,这是:

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

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

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

编辑

@caub(原@crl)提出了一个简单的组合,对于这两种查询字符串和哈希作品(尽管它使用正则表达式,如果任何人有这样的问题):

function getPathFromUrl(url) { 
    return url.split(/[?#]/)[0]; 
} 
+4

+1 ...在这种情况下,实际上split()比substring()更好。 – 2010-03-29 20:42:52

+1

但是,我还发现substring()方法与split()方法相比快近两倍。 (在Firefox 3.6中)。 – 2010-03-29 21:23:29

+7

边缘优化如果很重要(可能不是):'split('?',1)[0]'。 – bobince 2010-03-29 22:56:54

31

月2日更新:在试图提供一个全面的答案,我在标杆的各种答案提出的三种方法。

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'); 

结果在Firefox 3.5.8在Mac OS X 10.6.2:

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

结果在Chrome 5.0.307.11在Mac OS X 10.6.2:

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

注子字符串方法的功能比较差,因为如果URL不包含查询字符串,它将返回空字符串。如预期的那样,其他两种方法将返回完整的URL。不过有趣的是,子串方法是最快的,特别是在Firefox中。


1日更新:其实split()方法suggested by Robusto是一个更好的解决方案,在一个我前面建议的,因为即使在没有查询字符串它会工作:

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" 

Original Answer:

var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3'; 
testURL.substring(0, testURL.indexOf('?')); // Returns: "/Products/List" 
+9

O_O确实很全面,但是...为什么?最灵活和最合适的方法显然是胜利的,速度在这里完全不受关注。 – deceze 2010-03-29 23:57:18

+1

@deceze:只是为了好奇...而且因为在Angus的回答中有关于正则表达式方法性能的争论。 – 2010-03-30 00:03:38

+5

非常有趣,丹尼尔。如果我必须在页面上进行10K URL解析,我将寻求另一线工作。 :) – Robusto 2010-03-30 01:35:14

2

如果你到正则表达式。 ...

var newURL = testURL.match(new RegExp("[^?]+")) 
+1

正则表达式很慢 - 以简单快捷的方式进行。 – Aristos 2010-03-29 20:49:46

+0

不是这个操作,它不是。一个长URL上的1000个操作需要5ms在Firefox中 – plodder 2010-03-29 20:58:30

+1

@Angus:你的循环给你“一个脚本可能很忙......”,因为你忘了增加一个“++”。修复测试,在iMac 2.93Ghz Core 2 Duo上的Firefox 3.6中,RegEx为8ms,拆分方法为3ms,但答案为+1,因为它仍是另一种选择。 – 2010-03-29 21:20:36

3

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

public static String stripQueryStringAndHashFromPath(String uri) { 
return uri.replaceAll(("(\\?.*|\\#.*)"), ""); 
} 
1
var path = "path/to/myfile.png?foo=bar#hash"; 

console.log(
    path.replace(/(\?.*)|(#.*)/g, "") 
); 
-1

如果使用骨干。JS(其含有url anchor如路线),URL query string可能会出现:

  1. 之前url anchor

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

    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', ''); 
0

这可能是一个老问题,但我已经试过这种方法来删除查询参数。似乎工作顺利,因为我需要重新加载以及删除查询参数。

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

此外,因为我使用简单的字符串加法操作,我猜测性能会很好。但仍然值得与片段比较this answer

相关问题