最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get the current browser URL using JavaScript? - Stack Overflow

programmeradmin5浏览0评论

I have a website on a host example.myhost but I also have a sub-domain for French fr.example.myhost which is different than example.myhost.

If I'm at fr.example.myhost and I use window.location.href, I get example.myhost instead of fr.example.myhost.

How do I get the URL typed in the address bar of the browser?

I have a website on a host example.myhost.com but I also have a sub-domain for French fr.example.myhost.com which is different than example.myhost.com.

If I'm at fr.example.myhost.com and I use window.location.href, I get example.myhost.com instead of fr.example.myhost.com.

How do I get the URL typed in the address bar of the browser?

Share Improve this question edited Feb 5, 2012 at 22:33 Christofer Eliasson 33.9k7 gold badges76 silver badges103 bronze badges asked Feb 5, 2012 at 22:28 AlonAlon 3,90610 gold badges46 silver badges65 bronze badges 12
  • Do you see the right content if you run alert(location)? – Douglas Commented Feb 5, 2012 at 22:31
  • location.href should give you the entire url. – SeanCannon Commented Feb 5, 2012 at 22:33
  • @Douglas, no , the alert box gives example.myhost.com – Alon Commented Feb 5, 2012 at 22:33
  • @AlienWebguy, it doesn't give me the current url – Alon Commented Feb 5, 2012 at 22:34
  • 1 @Alon what browser are you using? – LordZardeck Commented Feb 5, 2012 at 22:34
 |  Show 7 more comments

3 Answers 3

Reset to default 15

window.location

The subdomain shouldn't make a difference. If, however, you've typed the URL in the address bar, after the page has loaded (but not hit enter), then that's a different story altogether.

That's weird, cause it works for me. You might try:

  • window.location.host
  • window.location.hostname
  • window.location.origin

Look the same question here : Get current URL in web browser

Use :

console.log(document.URL);
发布评论

评论列表(0)

  1. 暂无评论