js獲取當前頁面的url

2021-07-08 17:00:17 字數 583 閱讀 2991

設定或獲取物件指定的檔名或路徑。

設定或獲取整個 url 為字串。

設定或獲取與 url 關聯的埠號碼。

設定或獲取 url 的協議部分。

設定或獲取 href 屬性中在井號「#」後面的分段。

設定或獲取 location 或 url 的 hostname 和 port 號碼。

設定或獲取 href 屬性中跟在問號後面的部分。

window.location.host; 

//返回url 的主機部分,例如:www.***.com

window.location.hostname; //返回www.***.com

window.location.href; //返回整個url字串(在瀏覽器

window.location.pathname; //返回/a/index.php或者/index.php

window.location.protocol; //返回url 的協議部分,例如: http:,ftp:,maito:等等。

window.location.port //url 的埠部分,如果採用預設的80埠,那麼返回值並不是預設的80而是空字元

JS獲取當前頁面的URL

var url url window.location.href 獲取完整url alert url url window.location.pathname 獲取檔案路徑 檔案位址 alert url test index.html url window.location.protocol 獲取協...

獲取當前頁面的URL

window location host 返回url 的主機部分,例如 www.com window location hostname 返回www.com window location href 返回整個url字串 window location pathname 返回 a index.php或...

獲取當前頁面的url

獲取當前頁面的url,可以使用window.location.href 如果想進一步獲取到主機名,埠號等可以直接使用 window.location.host 主機名加埠號 window.location.hostname 主機名 window.location.origin 協議主機名埠號 win...