# location 對象
window.location 對象用于獲得當前頁面的地址URL,并把瀏覽器重定向到新的頁面。
## location對象的屬性
* location.hostname 返回web主機的域名
* location.pathname 放回當前頁面的路徑和文件名
* location.port 放回web主機的端口
* location.protocol 返回所使用的web協議 [http|https]
* location.href 屬性返回當前頁面的URL
* location.assign() 方法加載新的文檔
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>window.location對象</title>
</head>
<body>
<button id="btn">按鈕</button>
<script>
function getLoc(){
console.log( window.location.hostname); //返回web主機域名
console.log( window.location.pathname); //返回web主機名
console.log( window.location.port); //放回web主機的端口
console.log( window.location.protocol); //返回所使用的web協議
console.log( window.location.href); //屬性返回當前頁面的URL
// location.assign("https://www.baidu.com"); // 加載新的文檔
}
var oBtn = document.getElementById('btn');
oBtn.addEventListener("click",getLoc);
</script>
</body>
</html>
~~~
- 空白目錄
- JavaScript保留字
- JS事件
- JS面向對象
- JS內置對象
- 自定義對象
- String 字符串對象
- Date 日期時間對象
- Array 數組對象
- Math 對象
- DOM對象控制HTML
- getElementsByName
- getElementsByTagName
- getAttribute 獲取元素屬性
- setAttribute 設置元素屬性
- childNodes 訪問子節點
- parentNode 訪問父節點
- createElement 創建元素節點
- createTextNode 創建文本節點
- insertBefore 插入節點
- removeChild 刪除節點
- offsetHeight 網頁高度
- scrollHeight 網頁高度
- JS瀏覽器對象
- window對象
- 計時器
- history對象
- location對象
- screen對象
- navigator對象
- 彈出窗口
- cookies