如何使用 JavaScript 获取当前浏览器 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9153686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current browser URL using JavaScript?
提问by Alon
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.
我在主机上有一个网站,example.myhost.com
但我也有一个法语子域,fr.example.myhost.com
它与 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
.
如果我在fr.example.myhost.com
并使用window.location.href
,我会得到example.myhost.com
而不是fr.example.myhost.com
。
How do I get the URL typed in the address bar of the browser?
如何获取浏览器地址栏中输入的 URL?
回答by vol7ron
window.location
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.
子域不应该有什么不同。但是,如果您在页面加载后(但未按 Enter 键)在地址栏中键入 URL,则情况完全不同。
回答by LordZardeck
That's weird, cause it works for me. You might try:
这很奇怪,因为它对我有用。你可以试试:
- window.location.host
- window.location.hostname
- window.location.origin
- 窗口.位置.主机
- 窗口.位置.主机名
- 窗口.位置.原点
回答by Ema.H
Look the same question here : Get current URL in web browser
在此处查看相同的问题:在 Web 浏览器中获取当前 URL
Use :
利用 :
console.log(document.URL);