測試網址: http://localhost:63343/2023-01-22.html?Q1=1&Q2=2#tag
取得完整網址
取得網址中的路徑名稱
取得網址中的查詢字串
取得網址中的查詢參數
取得網址中的錨點 (hash)
取得網址中的通訊協議 (protocol)
取得網址中的主機 (host)
取得網址中的主機名稱 (hostname)
取得網址中的連接埠 (port)
取得完整網址
    
console.log(window.location.href); // http://localhost:63343/2023-01-22.html?Q1=1&Q2=2#tag
    
取得網址中的路徑名稱
    
console.log(window.location.pathname); // /2023-01-22.html
    
取得網址中的查詢字串
    
console.log(window.location.search); // ?Q1=1&Q2=2
    
取得網址中的查詢參數
    
console.log(window.location.searchParams); // URLSearchParams { 'Q1' => '1', 'Q2' => '2' }
    
取得網址中的錨點 (hash)
    
console.log(window.location.hash); // #tag
    
取得網址中的通訊協議 (protocol)
    
console.log(window.location.protocol); // http:
    
取得網址中的主機 (host)
    
console.log(window.location.host); // localhost:63343
    
取得網址中的主機名稱 (hostname)
    
console.log(window.location.hostname); // localhost
    
取得網址中的連接埠 (port)
    
console.log(window.location.port); // 63343
    
留言
張貼留言
如果有任何問題、建議、想說的話或文章題目推薦,都歡迎留言或來信: a@ruyut.com