2010-03-29 153 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

相關問題