获取当前页面域名的几种方式:

(1)方法一:

<script type="text/javascript">
hname=window.location.host;
function disp_alert()
{alert(hname);}
</script>

当前页面的运行结果为:

www.yuucn.com

(2)方法二:

<script type="text/javascript">
hname=document.domain;
function disp_alert()
{alert(hname);}
</script>

当前页面的运行结果为:

yuucn.com

(3)方法三:

<script type="text/javascript">
hname=window.location.href;
function disp_alert()
{alert(hname);}
</script>

当前页面的运行结果为:

https://www.yuucn.com/a/1594551.html

总结:
window.location.host、document.domain、window.location.href三个方式获取的内容是不一样的,在选择的时候,根据自己想要的内容,使用相应的代码。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。