Is there a built-in way to get the current URL without any query parameters?

£可爱£侵袭症+ 提交于 2020-01-11 19:58:53

问题


If my URL is http://www.something.com/foo/bar/index.html?color=yellow&animal=rat, it seems as though:

  • $location.path() will return foo/bar/index.html
  • $location.absUrl() will return http://www.something.com/foo/bar/index.html?color=yellow&animal=rat
  • $location.url() will return foo/bar/index.html?color=yellow&animal=rat

Is there any function which will return http://www.something.com/foo/bar/index.html?

Or do I have to construct that myself with functions like protcol, host, port, etc. (or strip the query params off myself)?


回答1:


As far as I'm aware you have to construct it yourself. Not that you were asking how to construct it, but for those who are wondering:

var url = $location.absUrl().split('?')[0]



回答2:


Not that this eliminates the need to construct it by yourself, just another way to do the same. If you use window.location object, you can just say window.location.origin+window.location.pathname

window.location object has

host:"localhost.abc.com:8080"
hostname:"localhost.abc.com"
href:"http://localhost.abc.com:8080/quickpick/repossessions/?displayStr=Repossessions&from=%2F&page=1"(whole url)

origin:"http://localhost.abc.com:8080"
pathname:"/quickpick/repossessions/"
port:"8080"
protocol:"http:"


来源:https://stackoverflow.com/questions/23505577/is-there-a-built-in-way-to-get-the-current-url-without-any-query-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!