Javascript 获取网站 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19550136/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:57:05  来源:igfitidea点击:

Javascript Get Website URL

javascript

提问by k300

How do I get Javascript to tell me the website url.

我如何让 Javascript 告诉我网站 url。

For example if I have a page www.example.com/page.html

例如,如果我有一个页面 www.example.com/page.html

I want Javascript to tell me the site url is www.example.comand not www.example.com/page.html(which document.location tells me)

我希望 Javascript 告诉我网站 url 是www.example.com和不是www.example.com/page.html(哪个 document.location 告诉我)

Is there a way to do this? If so, how?

有没有办法做到这一点?如果是这样,如何?

Thanks in advance for your help :)

在此先感谢您的帮助 :)

回答by stewart715

There are several ways you can do this, but one way might be best for certain situations (e.g. within an iFrame).

有几种方法可以做到这一点,但一种方法可能最适合某些情况(例如在 iFrame 中)。

Protocol + Domain + Page

协议+域名+页面

document.URL
> "http://example.com/page1.html"

document.location.href
> "http://example.com/page1.html"


Protocol + Domain

协议+域

document.location.origin
> "http://example.com"


Domain

领域

document.location.host
> "example.com"


Page

document.location.pathname
> "/page1.html"

回答by Max

Try this

试试这个

document.location.host

回答by deepakborania

Use alert(window.location.origin) for getting the url.

使用 alert(window.location.origin) 获取 url。

回答by Jaime Torres

Try

尝试

document.location.origin

That will give you the protocol and host.

这将为您提供协议和主机。

回答by Shamps

Use

利用

window.location.hostname

You can test it by just typing it in the chrome dev tools console

您可以通过在 chrome 开发工具控制台中输入它来测试它

Reference

参考

MDN: https://developer.mozilla.org/en-US/docs/Web/API/Location

MDN:https: //developer.mozilla.org/en-US/docs/Web/API/Location

回答by Nishu Tayal

There are many ways to get this.
Open Chrome browser and press F12, you'll get console.

有很多方法可以得到这个。
打开 Chrome 浏览器并按 F12,您将获得控制台。

Type following commands there for the same question URL. You will get your answer

在那里为相同的问题 URL 键入以下命令。你会得到你的答案

window.location.hostname // Output : stackoverflow.com

window.location.origin // Output : http://stackoverflow.com

document.location.host // Output : stackoverflow.com

回答by Naved Ali

use

利用

document.location.origin+document.location.pathname;

where document.location.originwill redirect you to "http://www"and document.location.pathnamewill redirect you to "/stackoverflow/"(Name of your project). In this way you can give reference to page or post you want in your js file.Suppose if i want reference to my home page i would have use

在那里document.location.origin你会重定向到"http://www"document.location.pathname将您重定向到“/计算器/”(项目名称)。通过这种方式,您可以在 js 文件中引用您想要的页面或帖子。假设如果我想引用我的主页,我会使用

var address=document.location.origin+document.location.pathname;
 window.location.replace(address+"/home");

So using above example i can easily redirect to my homepage

所以使用上面的例子我可以很容易地重定向到我的主页