獲取當前頁面域名的幾種方式

獲取當前頁面域名的幾種方式:

(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三個方式獲取的內容是不一樣的,在選擇的時候,根據自己想要的內容,使用相應的代碼。

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