问题?javascript之js window详解
定义:JavaScript Window - 浏览器对象模型
浏览器对象模型 (BOM)
浏览器对象模型(Browser Object Model)尚无正式标准。
由于现代浏览器已经(几乎)实现了 JavaScript 交互性方面的相同方法和属性,因此常被认为是 BOM 的方法和属性。
1.Window 对象
所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:
window.document.getElementById("header");与此相同:
document.getElementById("header");2.Window 尺寸
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
立即学习“Java免费学习笔记(深入)”;
window.innerHeight - 浏览器窗口的内部高度 window.innerWidth - 浏览器窗口的内部宽度
对于 Internet Explorer 8、7、6、5:
document.documentElement.clientHeight document.documentElement.clientWidth
或者
这个是得到body的长宽
document.body.clientHeight document.body.clientWidth
如:
var w = window.innerWidth;
var h = window.innerHeight;
/*这个获取的长宽与下列的相同*/
var w2 = document.documentElement.clientWidth;
var h2 = document.documentElement.clientHeight;
/*这得到的是body的长度*/
var h1 = document.body.clientHeight;
var w1 = document.body.clientWidth;
document.getElementById("a1").innerHTML="内部窗口大小为:"+w+"*"+h+" "+w1+"*"+h1+" "+w2+"*"+h2;3.其他 Window 方法
一些其他方法:
window.open() - 打开新窗口
如:
window.open('1.html','ss');
window.close() - 关闭当前窗口window.moveTo() - 移动当前窗口window.resizeTo() - 调整当前窗口的尺寸如:
window.resizeTo(600,600);
4.localtion
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。
Window Location
window.location 对象在编写时可不使用 window 这个前缀。
一些例子:
location.hostname 返回 web 主机的域名 location.pathname 返回当前页面的路径和文件名 location.port 返回 web 主机的端口 (80 或 443) location.protocol 返回所使用的 web 协议(http:// 或 https://)
例子:
document.getElementById("a2").innerHTML="主机名"+location.hostname+"
路径:"+location.pathname+" 端口号:"+location.port+" web协议:"+location.protocol;例子:
document.getElementById("a2").innerHTML="主机名"+location.hostname+"
路径:"+location.pathname+" 端口号:"+location.port+" web协议:"+location.protocol+" IRL:"+location.href;
5.Window Location Assign
location.assign() 方法加载新的文档。
例子:
window.location.assign("http://www.w3school.com.cn");
window.location.assign("1.html");6.Window History
window.history 对象在编写时可不使用 window 这个前缀。
为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。
一些方法:
history.back() - 与在浏览器点击后退按钮相同
history.forward() - 与在浏览器中点击按钮向前相同
7.window.navigator 对象包含有关访问者浏览器的信息。
Window Navigator
window.navigator 对象在编写时可不使用 window 这个前缀。
实例
