Caveats:
location.origin
is not supported by IE.
Other improvements: .slice
is actually calling Array.prototype.slice
. A method call that requires a prototype lookup is bound to be slower than accessing the element you need directly, escpeciallly in your case, where the slice
method is returning an array with just 1 element anyway. So:
You could use location.pathname
, but be weary: the standard reads:
pathname
This attribute represents the path component of the Location's URI which consists of everything after the host and port up to and excluding the first question mark (?) or hash mark (#).
but I think the easiest, most X-browser way of getting what you want is actually simply doing this:
var queryString = location.href.split(location.host)[1];
//optionally removing the leading `/`
var queryString = location.href.split(location.host)[1].replace(/^\//,'');
It's very similar to what you have now, except for the fact that I'm not using location.origin
, which, as shown on MDN is not supported by MS's IE...
Another benefit is that I'm not calling Array.prototype.slice
, which returns an array, and requires a prototype-lookup, which is marginally slower, too...