js獲取域名的方法

說明

本文實例講述了js獲取域名的方法,分享給大家供大家參考,具體實現方法如下:

代碼示例

<script>
//獲取域名
var host = window.location.host;
var host2 = document.domain;
//獲取頁面完整地址
var url = window.location.href;
</script>

運行結果如下:

host:www.wicode.cn
host2:www.wicode.cn
url:http://www.wicode.cn/article/60420.html

補充

location.hostname 返回 web 主機的域名
location.pathname 返回當前頁面的路徑和文件名
location.port 返回 web 主機的端口 (80 或 443)
location.protocol 返回所使用的 web 協議(http:// 或 https://)

希望本文對大家的開發有所幫助。

如果幫到你了,請點贊轉發哦~

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