Current URL without parameters, hash, http(s)://

2019-01-22 11:38发布

I'm looking for a neat way of getting the URL of the current document in Javascript.

  • The URL should be clean of parameters (?parameter1=bla&parameter2=bla)
  • The URL should be clean of hash tags (#jumppoint)
  • http/https should be removed/consolidated into http

I know i can get the current URL with location.href and then use some regular expressions to clean it up but maybe there is a nicer/cleaner solution for getting rid of the junk?

8条回答
ゆ 、 Hurt°
2楼-- · 2019-01-22 12:17

This page indicates that you could probably use window.location.host to get the part you're actually interested in. I haven't tested it, though.

查看更多
3楼-- · 2019-01-22 12:19

Try:


window.location.hostname;

查看更多
登录 后发表回答