HTML URL地址解析


通過JavaScript的location對象,可獲取URL中的協議、主機名、端口、錨點、查詢參數等信息。

示例

URLhttp://www.akmsg.com/WebDemo/URLParsing.html#top?username=admin&pwd=123456

解析結果: 

屬性名稱 獲取的值 說明
location.hash #top?username=admin&pwd=123456 URL中的的錨點部分,包含開頭的#符號
location.host www.akmsg.com 主機名稱和端口
location.hostname www.akmsg.com 主機名稱
location.href http://www.akmsg.com/WebDemo/URLParsing.html#top?username=admin&pwd=123456 完整的URL
location.pathname /WebDemo/URLParsing.html 路徑部分
location.port   端口
location.protocol http: 協議,最后面會有個':'冒號
location.search   URL的查詢部分(從問號 (?) 開始的 URL)
注意:當URL含有錨點時,此處返回空字符。
location.origin http://www.akmsg.com URL的源。返回格式:協議+主機名+端口
location.origin+location.pathname http://www.akmsg.com/WebDemo/URLParsing.html URL的訪問地址。返回格式:協議+主機名+端口+路徑部分

代碼

console.log(
	'location.hash     :' + location.hash     + '\r\n' +
	'location.host     :' + location.host     + '\r\n' +
	'location.hostname :' + location.hostname + '\r\n' +
	'location.href     :' + location.href     + '\r\n' +
	'location.pathname :' + location.pathname + '\r\n' +
	'location.port     :' + location.port     + '\r\n' +
	'location.protocol :' + location.protocol + '\r\n' +
	'location.hash     :' + location.hash     + '\r\n' +
	'location.search   :' + location.search   + '\r\n' +
	'location.origin   :' + location.origin 
)

 

在線示例

地址http://www.akmsg.com/WebDemo/URLParsing.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM