DOM编程-window对象 回顾 请简述一下脚本执行的原理。
open()
// 语法window.open("url","target","features")url:指定的网址target:打开新标签页的方式 _blank 打开新窗口(默认) _self 原地打开features:指定打开的窗口大小的参数 'height=400px,width=400px'
close()
window.close();
setTimeout()
setTimeout(function(){ // 执行代码}, 100); // 1秒后执行setTimeout(function () { console.log("等会见") }, 100); //1秒后执行
setInterval()
setInterval(function(){ // 执行代码}, 200); // 每2秒执行一次setInterval(function () { console.log("执行中...") }, 200); // 每2秒执行一次
alert()
window.alert("Hello, World!");
confirm()
if (window.confirm("Are you sure?")) { // 用户点击了确认按钮} else { // 用户点击了取消按钮}if (window.confirm("Are you sure?")) { window.alert("yes") } else { window.alert("no") }
prompt()
let name = window.prompt("Please enter your name:");
let windowHeight = window.innerHeight;console.log(windowHeight); // 输出当前窗口的视口高度
let windowWidth = window.innerWidth;console.log(windowWidth); // 输出当前窗口的视口宽度
window.addEventListener("resize", function() { let windowHeight = window.innerHeight; let windowWidth = window.innerWidth; console.log("Window Height:", windowHeight); console.log("Window Width:", windowWidth); });
是指打开当前窗口的那个窗口对象。它是 Window 对象的一个属性,用于在当前窗口中引用到打开它的父窗口或者来源窗口。
在以下情况下可以使用 window.opener
let popup = window.open("popup.html"); // 打开一个弹出窗口
let parentWindow = window.opener;console.log(parentWindow); // 输出父窗口对象
let currentWindow = window.open("current.html"); // 打开当前窗口// 向当前窗口发送消息currentWindow.postMessage("Hello", "http://example.com");
window.addEventListener("message", function(event) { let message = event.data; let sourceWindow = event.source; let openerWindow = window.opener; console.log("Message:", message); console.log("Source Window:", sourceWindow); console.log("Opener Window:", openerWindow); });
window.postMessage("Hello!");
window.location // 控制地址window.history // 控制浏览器历史记录window.navigator // 获取浏览器标识信息
let pageTitle = window.document.title;console.log(pageTitle);
window.location.href = 'https://example.com';
let currentURL = window.location.href;console.log(currentURL);// 跳转到目标地址window.href = "url地址"
let protocol = window.location.protocol;console.log(protocol);
let host = window.location.host;console.log(host);
let hostname = window.location.hostname;console.log(hostname);
let port = window.location.port;console.log(port);
let pathname = window.location.pathname;console.log(pathname);
let searchParams = window.location.search;console.log(searchParams);
let hash = window.location.hash;console.log(hash);
// 将当前页面重定向到 example.comwindow.location.assign("http://example.com");
// 重新加载当前页面window.location.reload();
此子对象用于操作浏览器的历史记录,包括向前和向后导航。
例如,要后退到上一个页面:
window.history.back();
let historyLength = window.history.length;console.log(historyLength);
window.history.back();
window.history.forward();
// 后退两个历史记录window.history.go(-2);// 前进一个历史记录window.history.go(1);// 重新加载当前页window.history.go();
// 添加新的历史记录状态window.history.pushState({ page: 1 }, "Page 1", "/page1.html");
// 替换当前历史记录状态window.history.replaceState({ page: 2 }, "Page 2", "/page2.html");
let userAgent = window.navigator.userAgent;console.log(userAgent);
let userAgent = window.navigator.userAgent;console.log(userAgent);
let platform = window.navigator.platform;console.log(platform);
let vendor = window.navigator.vendor;console.log(vendor);
let language = window.navigator.language;console.log(language);
let cookieEnabled = window.navigator.cookieEnabled;console.log(cookieEnabled);
let plugins = window.navigator.plugins;console.log(plugins);