首先,先把獲取各參數的方式再寫一遍,相信大家都耳熟能詳,就寫幾個常用的吧。
以此網址https://i.cnblogs.com/EditPosts.aspx?opt=1為例:
1. var url=window.location.href
console.log(url)-->https://i.cnblogs.com/EditPosts.aspx?opt=1
獲取完整url,包含所有參數。
2. var url=window.location.search
console.log(url)-->opt=1
獲取問號后面的內容
3. var url=window.location.hash
console.log(url)-->額,這個url中沒有hash
這是獲取#后面的信息,就是錨點、哈希。
4. var url=window.location.protocol
console.log(url)-->https:
這是獲取頁面的協議,或者http或者https或者ftp
5. var url=window.location.host
console.log(url)-->i.cnblogs.com
獲取頁面url的端口號和hostname,也可以寫window.location.hostname獲取主機名稱信息
常用的就這幾個吧,還有幾個沒用到過不敲了,至於為什么會獲取到呢,,正好在慕課網node.js視頻中看到url對象,
這些都在圖里了。