location指示了其所鏈接對象的url位置。Document和window對象中都有location屬性,能夠經過window.location和document.location訪問。
注意 若是想要得到當前文檔的完整url字符串,有四種方式html
當前文檔的完整url,若是被改變,文檔將會導航到另外一個新的頁面,瀏覽器
// 網址 "https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils.protocol"; location.href = https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils.protocol
當前url所使用的協議,包括結尾的":"緩存
// 網址 "https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils.protocol"; location.protocol = https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils.protocol
獲取當前的主機信息,包括主機名,":"和端口號
舉例 :服務器
// 網址 "https://developer.mozilla.org:4097/en-US/HTMLHyperlinkElementUtils.host"; anchor.host == "developer.mozilla.org:4097"
注意 當服務器使用的端口爲默認端口時,則返回的host信息不包括:port
post
// 網址 "https://developer.mozilla.org:443/en-US/HTMLHyperlinkElementUtils.host"; location.host == "developer.mozilla.org"
獲取當前url的主機名url
// 網址 "https://developer.mozilla.org:443/en-US/HTMLHyperlinkElementUtils.host"; location.host == "developer.mozilla.org"
返回url的端口信息。沒有寫端口信息的url,實際端口爲與協議相關的端口號.net
// 網址 "https://developer.mozilla.org:443/en-US/HTMLHyperlinkElementUtils.host"; location.port = "443"
返回url的路徑字符串code
// 網址 "https://developer.mozilla.org:443/en-US/HTMLHyperlinkElementUtils.host"; location.pathname = "/en-US/HTMLHyperlinkElementUtils.host";
注意這裏包括最前面的/
和最後面的index.html
htm
又名查詢字符串,返回url中?以及以後的字符串對象
// 網址爲 "https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.search?q=123" location.search = '?q=123'; //將去掉問號後的字符串解析爲URLSearchParams對象 let params = new URLSearchParams(location.search.substring(1)); //利用get方法獲取指定的參數 let q = parseInt(params.get("q")); // is the number 123
返回url中表明頁面某個區域的帶有#的字符串
//網址 "https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.href#youhou"; location.hash = '#youhou';
設置或返回url中域名前面的用戶名
// 網址 "https://anonymous:flabada@developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.username" location.username = 'anonymous';
設置或返回url中密碼部分
// 網址"https://anonymous:flabada@developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.username" location.password = 'flabada';
返回url中完整的協議和主機地址部分,包括端口
//網址https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/origin location.origin = 'https://developer.mozilla.org';
var url = document.location; url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container'; console.log(url.href); // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container console.log(url.protocol); // https: console.log(url.host); // developer.mozilla.org console.log(url.hostname); // developer.mozilla.org console.log(url.port); // (blank - https assumes port 443) console.log(url.pathname); // /en-US/search console.log(url.search); // ?q=URL console.log(url.hash); // #search-results-close-container console.log(url.origin); // https://developer.mozilla.org
該方法使瀏覽器加載並展現URL所指定的文檔
document.location.assign('https://developer.mozilla.org/en-US/docs/Web/API/Location.reload');
該方法用於從新加載當前頁面,能夠接受一個Boolean類型的參數,參數爲true,強制從服務器從新獲取,爲false時從緩存中讀取。默認值爲false
document.location.reload(true);
提供一個URL,使頁面跳轉到相應的URL,與location.assign()的區別是,location.replace()跳轉後的頁面不會保存在瀏覽器歷史中,即沒法經過返回按鈕返回到該頁面。
document.location.replace('https://developer.mozilla.org/en-US/docs/Web/API/Location.reload');
獲取當前頁面的完整URL,至關於location.href