location 引數詳解 例項展示
阿新 • • 發佈:2018-12-11
- location.href 設定或返回完整的 URL。在瀏覽器的位址列上怎麼顯示它就怎麼返回。
- location.protocol:協議名.取值有'https:','https:','file:' 等等。
- location.host=location.hostname+location.port:主機名+埠號
- location.hostname:主機名
- location.port:埠號
- location.pathname:路徑
- location.search:查詢串.從問號 (?) 開始的 URL(查詢部分)
- location.hash:書籤名 設定或返回從井號 (#) 開始的 URL(錨:a標籤中設定的href值)。如果地址裡沒有“#”,則返回空字串。
栗子來了-------假設有這麼一個地址:
location.protocol // "https:" 注意帶有冒號":"
location.hostname // "www.baidu.com"
location.port // "8080" 如果採用預設的80埠(即使添加了:80 如:www.baidu.com:80),那麼返回值並不是預設的80而是空字元 ""
location.pathname // "/js/index.html" 注意js前面的斜槓"/"要帶上
location.hash // "#here" 如果地址裡沒有“#”,則返回空字串。