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

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

(1)方法一:

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

当前页面的运行结果为:

blog.csdn.net

(2)方法二:

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

当前页面的运行结果为:

csdn.net

(3)方法三:

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

当前页面的运行结果为:

http://blog.csdn.net/qq_34648000/article/details/52268452

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章