Javascript获取网站URL [英] Javascript Get Website URL

查看:109
本文介绍了Javascript获取网站URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

是有办法做到这一点?如果是这样,怎么样?

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

在此先感谢您的帮助:)

Thanks in advance for your help :)

推荐答案


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

协议+域名+页面

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

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






协议+域名

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






document.location.host
> "example.com"






Page

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

这篇关于Javascript获取网站URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆