views:

500

answers:

4

What is an easy way to remove the querystring from a Path in Javascript? I have seen a plugin for Jquery that uses window.location.search. I can not do that: The URL in my case is a variable that is set from AJAX.

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

2nd Update: In attempt to provide a comprehensive answer, I am benchmarking the three methods proposed in the various answers.

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

Results in Firefox 3.5.8 on Mac OS X 10.6.2:

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

Results in Chrome 5.0.307.11 on Mac OS X 10.6.2:

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

Note that the substring method is inferior in functionality as it returns a blank string if the URL does not contain a querystring. The other two methods would return the full URL, as expected. However it is interesting to note that the substring method is the fastest, especially in Firefox.


1st UPDATE: Actually the split() method suggested by Robusto is a better solution that the one I suggested earlier, since it will work even when there is no 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"

Original Answer:

var testURL = '/Products/List?SortDirection=dsc&Sort=price&Page=3&Page2=3';
testURL.substring(0, testURL.indexOf('?'));    // Returns: "/Products/List"
Daniel Vassallo
O_O Very comprehensive indeed, but… why? The most flexible and appropriate method obviously wins, speed is of absolutely no concern here.
deceze
@deceze: Just for curiosity... And because there was an argument about the performance of the regex method in the comments of the Angus' answer.
Daniel Vassallo
Very interesting, Daniel. And if I ever have to do 10K URL parses on a page I'm going to seek another line of work. :)
Robusto
A: 

If you need to perform complex operation on URL, you can take a look to the jQuery url parser plugin.

Boris Guéry
+10  A: 

An easy way to get this is:

function getPathFromUrl(url) {
  return url.split("?")[0];
}
Robusto
+1... Actually split() is better than substring() in this case.
Daniel Vassallo
However, I also discovered that the substring() method is nearly twice as fast when compared to the split() method. (in Firefox 3.6).
Daniel Vassallo
Marginal optimisation if it matters (probably not): `split('?', 1)[0]`.
bobince
+1  A: 

If you're into RegEx....

var newURL = testURL.match(new RegExp("[^?]+"))
plodder
the regexp is slow one - go with the easy fast way.
Aristos
Not for this operation it isn't. 1000 ops on a long URL takes 5ms in firefox
plodder
@Angus: Your loop is giving you "A script may be busy..." because you forgot to increment `a++;`. Fixing the tests, in my Firefox 3.6 on an iMac 2.93Ghz Core 2 Duo, I get 8ms for the RegEx, and 3ms for the split method... Nevertheless +1 for the answer, because it is still another option.
Daniel Vassallo
thanks - I fixed it a few mins ago! - but the point is when you are talking 0.000005 seconds even for the reg exp operation, performance is not an issue for any solution :-)
plodder
`match()` returns an object. You probably don't want that. Call `toString()` on it (or `+''`).
bobince
Bob - good catch. Actually it returns an array of matches - an Array of strings in this case. So testURL.match(new RegExp("[^?]+"))[0] does it
plodder