admin管理员组文章数量:1391999
What is the best way to get the "anything" part after the domain part, using Javascript:
For I would have to use window.location.hash. But for I would have to use window.location.pathname.
I'm using:
window.location.href.replace(window.location.origin, "").slice(1)
Are there any caveats with this solution? Is there a better way?
What is the best way to get the "anything" part after the domain part, using Javascript:
http://www.domain./anything
http://www.domain./#anything
http://www.domain./any/thing
For http://www.domain./#anything I would have to use window.location.hash. But for http://www.domain./anything I would have to use window.location.pathname.
I'm using:
window.location.href.replace(window.location.origin, "").slice(1)
Are there any caveats with this solution? Is there a better way?
Share Improve this question edited Jul 21, 2014 at 11:36 tshepang 12.5k25 gold badges97 silver badges139 bronze badges asked Oct 1, 2013 at 9:21 TechAurelianTechAurelian 5,8216 gold badges53 silver badges68 bronze badges2 Answers
Reset to default 5window.location.pathname + window.location.search + window.location.hash
I think this one is a little bit better. You dont have to use any functions here...
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 ponent 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...
本文标签: How to get anything following the domain in a urlusing JavaScriptStack Overflow
版权声明:本文标题:How to get anything following the domain in a url, using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742066308a2418855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论