一. window 對象
1.打開一個新窗口: open() 和 關閉一個窗口:close()
var winOpen;
//打開一個新的窗口
function winOpen(){
winOpen = window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes')
}
//關閉一個瀏覽器窗口
function winClose(){
winOpen.close()
}
- 提示框: alert() prompt() confirm()
//顯示帶有一段消息和一個確認按鈕的警告框
function winAlert(){
window.alert("我只是一段測試文字");
}
//顯示可提示用戶輸入的對話框
function winPrompt(){
window.prompt("請輸入名字","這里輸入密碼");
}
//顯示帶有一段消息以及確認按鈕和取消按鈕的對話框
function winConfirm(){
window.confirm("請完善您的信息");
}
- 開啟和關閉計時器:1. setInterval() clearInterval() ------ 2.setTimeout() clearTimeout()
第一種方法:
var interval;
//開啟一個計時器setInterval()
function openInterval(){
interval = setInterval("clock()",100);
}
//關閉計時器
function closeInterval(){
clearInterval(interval);
alert("計時器關閉!!");
}
第二種方法
var time;
//開始計時器
function openTimeout(){
document.getElementById("con1").value = num;
document.getElementById("con").disabled = true;
if(num == 0){
document.getElementById("con").disabled = false;
num = 10;
}else{
time = setTimeout("openTimeout()",1000);
}
num = num + 1;
}
//取消計時器
function closeTimeout(){
clearTimeout(time);
document.getElementById("con").disabled = false;
}
- History 對象
document.write(window.history.length + "<br>"); //返回瀏覽器中歷史列表中的URL數量
//加載上一個頁面
window.history.back();
//加載下一個頁面
window.history.forward();
//go()方法,根據當前所處的頁面,加載 history 列表中的某個具體的頁面。
window.history.go(0);
5.Location 對象
document.write("href: " + window.location.href + "<br>"); // 完整的URL地址
document.write("hash: " + window.location.hash + "<br>"); // 從#號開始的URL(錨)
document.write("host: " + window.location.host + "<br>"); //主機名或當前URL的端口號
document.write("hostname: " + window.location.hostname + "<br>"); //當前URL的主機名
document.write("pathname: " + window.location.pathname + "<br>"); //當前URL的路徑部分
document.write("port: " + window.location.port + "<br>"); // 當前URL的端口號
document.write("protocol: " + window.location.protocol + "<br>"); //當前URL的協議
document.write("search: " + window.location.search + "<br>"); 從?號開始的URL(查詢部分)
6.Navigator 對象
document.write("appCodeName: " + window.navigator.appCodeName + "<br>"); //瀏覽器代碼名的字符串表示
document.write("appName: " + window.navigator.appName + "<br>"); //返回瀏覽器名稱
document.write("appVersion: " + window.navigator.appVersion + "<br>"); //返回瀏覽器的平臺和版本信息
document.write("platform: " + window.navigator.platform + "<br>"); //返回運行瀏覽器的操作系統平臺
document.write("userAgent: " + window.navigator.userAgent + "<br>"); // 返回由客戶機發送服務器的user-agent頭部的值
7.screen 對象
document.write("availHeight: " + window.screen.availHeight + "<br>"); // 窗口可以使用的屏幕高度,單位像素
document.write("availWidth: " + window.screen.availWidth + "<br>"); //窗口可以使用的屏幕寬度,單位像素
document.write("colorDepth: " + window.screen.colorDepth + "<br>"); //用戶瀏覽器表示的顏色位數,通常為 32位
document.write("pixelDepth: " + window.screen.pixelDepth + "<br>"); //用戶瀏覽器表示的顏色位數,通常為 32位
document.write("height: " + window.screen.height + "<br>"); //屏幕的高度
document.write("width: " + window.screen.width + "<br>"); //屏幕的寬度