Javascript: get window.location everything but host?
i have like
http://www.mydomain.com/hello/you
with top.location.host
, i can get "http://www.mydomain.com"
with window.location.href
i can get "http://www.mydomain.com/hello/y开发者_如何学编程ou"
is there a chance to get just "/hello/you"
???
location.pathname
pathname
will only return the path. If you want the querystring
and optionally hash
, you would need to combine the search
and hash
properties as well. Consider this url:
http://www.example.com/path/to/glory?key=value&world=cup#part/of/page
location.pathname => "/path/to/glory"
location.search => "?key=value&world=cup"
location.hash => "#part/of/page"
If you want the entire thing,
/path/to/glory?key=value&world=cup#part/of/page
then just concatenate all these:
location.pathname + location.search + location.hash
Always wanted to use with
somewhere. This looks like the perfect opportunity :)
with(location) {
pathname + search + hash;
}
Another approach would be excluding the protocol and host from the entire href using substring.
window.location.href.substring(
(window.location.protocol+'//'+window.location.host).length
)
If your url is http://google.com/test?whatever=1#hello
it will return /test?whatever=1#hello
.
精彩评论