window
window 是瀏覽的一個實例,在瀏覽器中window對象有雙重角色,即是通過javascript訪問瀏覽器窗口的接口,也是ECMAScript中規定的Global對象
所有全局變量和全局函數其實相當于window對象的屬性和方法
//聲明一個全局的變量
window.username="marry";//等價于var username="marry";
//聲明一個全局方法(全局方法在腳本的任何一個地方都可以調用)
window.sayName=function(){}
window對象的方法
window.alert("content") 顯示帶有一段消息和一個確認按鈕的警告框
window.confirm("message") 顯示一個帶有指定消息和OK及取消按鈕的對話框 返回值(布爾值)文本不能用<br>換行應使用\n
window.prompt("text","defaultText") 返回值:用戶單擊取消按鈕,返回null, 確認按鈕,則返回當前輸入的文本
window.open(pageURL, name, parameters) 打開一個新的瀏覽器窗口或使一個已命名并打開的窗口獲得焦點(或查找一個已命名的窗口)
window.close() 方法用于關閉瀏覽器窗口,也可以關閉其他窗口參考//(https://www.runoob.com/jsref/met-win-close.html)
window.open 更多參考
<body>
<a href="https:www.baidu.com" target="newopen">在newopen中打開這個鏈接</a>
<br>
<button id="btn">打開新窗開</button>
<script>
// width寬度,height高度,left左距離,top上距離,toolbar 工具欄=no,menubar 菜單欄=no,scrollbars 滾動條=no,location 地址=no,status 狀態欄=no
window.onload = function(){
btn.addEventListener('click',function(){
window.open('newopen.html','newopen',"width=400,height=400,status=1,left="+(window.screen.width/2-200)+",top="+(window.screen.height/2-200));
})
}
</script>
</body>
location
location即是window也是document的屬性 window.location==document.location
location.host 返回主機地址
location.pathname 返回URL中的目錄和文件名
location.port 返回URL中指定的端口號,如果沒有放回空字符串
location.protocol 返回頁面使用的協議 (http或者https)
location.search 返回URL的查詢字符串,這個字符串以問號開頭
location.href 返回完整的URL地址
location.hash 設置或返回從井號 (#) 開始的 URL(錨)
location的方法
location.href = window.location = documen.location.href 在歷史記錄中生成新紀錄
location.replace()
重新定向URL,不會在歷史記錄中生成新紀錄,即不能回退,沒有歷史記錄。loaction.href='xxx'會有歷史記錄
location.reload()
重新加載當前顯示的頁面。
location.reload()有可能從緩存中加載
location.reload(true)從服務器重新加載。
history
history.back() 后退一步 等同于 history.go(-1)
history.forward() 前進一步 等同于 history.go(1)
history.go() 正數向前 負數后退
screen
screen.availWidth 返回屏幕顯示的可用寬度(指除了任務欄之外的內容)
screen.availHeight 返回屏幕顯示的可用高度(指除了任務欄之外的內容)
獲取窗口文檔顯示區的寬高可以使用window.innerWidth和window.innerHeight
navigator 代表的當前瀏覽器的信息,通過該對象可以來識別不同的瀏覽器
navigator 代表的當前瀏覽器的信息,通過該對象可以來識別不同的瀏覽器
navigator.userAgent 一般我們只會使用userAgent來判斷瀏覽器的信息
/*
* 火狐的userAgent
* Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
* Chrome的userAgent
* Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
* IE8
* Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
* IE9
* Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
* IE10
* Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
* IE11
* Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko
*/
/*
* 在IE11中已經將微軟和IE相關的標識都已經去除了,所以我們基本已經不能通過UserAgent來識別一個瀏覽器是否是IE了
* 如果通過UserAgent不能判斷,還可以通過一些瀏覽器中特有的對象,來判斷瀏覽器的信息
* 比如:ActiveXObject
*/
var ua = navigator.userAgent;
if(/firefox/i.test(ua)){
alert("你是火狐?。?!");
}else if(/chrome/i.test(ua)){
alert("你是Chrome");
}else if(/msie/i.test(ua)){
alert("你是IE瀏覽器~~~");
}else if("ActiveXObject" in window){
alert("你是IE11,槍斃了你~~~");
}
document
event