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

来源:互联网 发布:广州大塘附近淘宝培训 编辑:程序博客网 时间:2024/06/08 08:41

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

(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三个方式获取的内容是不一样的,在选择的时候,根据自己想要的内容,使用相应的代码。

0 0
原创粉丝点击