1. 設置或獲取整個 URL 為字符串 var url=window.location.href 返回結果:url https://www.baidu.com/ 2. 設置或獲取 URL 的協議部分 var protocol= window.location.protocol; 返回結果: http: 3.設置或獲取主機 var host =window.location.host 返回結果: www.baidu.com 4.設置或獲取url端口 var port =window.location.port 返回結果: " " (如果采用默認的80端口(update:即使添加了:80),那么返回值並不是默認的80而是空字符) 5. 設置或獲取與 URL 的路徑部分 url="http://echarts.baidu.com/option.html"; var pathname= window.location.pathname; 返回結果:"/option.html" 6. 設置或獲取 href 屬性中跟在問號后面的部分 url="http://localhost:63342/project/6-21/aa.html?_ijt=flc0nna93fr89j3dlhh9mn7eco" var param= window.location.search; 返回結果: "?_ijt=flc0nna93fr89j3dlhh9mn7eco" 7. 設置或獲取 href 屬性中在井號“#”后面的分段 url="http://echarts.baidu.com/option.html#title.left" var param= window.location. hash; 返回結果: "#title.left"