Window 对象
所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:
window.document.getElementById("header");
document.getElementById("header");
Window 尺寸
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
Location 对象
history对象
history.back() - 与在浏览器点击后退按钮相同
history.forward() - 与在浏览器中点击按钮向前相同
所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:
window.document.getElementById("header");
document.getElementById("header");
Window 尺寸
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
window.innerHeight - 浏览器窗口的内部高度
window.innerWidth - 浏览器窗口的内部宽度
对于 Internet Explorer 8、7、6、5:
document.documentElement.clientHeight
document.documentElement.clientWidth
或者
document.body.clientHeight
document.body.clientWidth
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
function getWindosHeightAndWeight(){
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
alert("浏览器当前的宽度是:" + width);
alert("浏览器当前的高度是:" + height);
}
Location 对象
function getWindosHeightAndWeight(){
document.write("url:" +location.href + "<br>");
document.write("域名:" +location.hostname + "<br>");
document.write("请求文件的路径:" +location.pathname + "<br>");
document.write("端口号:" +location.port + "<br>");
document.write("协议:" +location.protocol + "<br>");
//把当前的页面跳转到百度首页上去
// location.href = "http://www.baidu.com";
// window.location.assign方法的作用相当于上句修改location的href属性
window.location.assign("http://www.baidu.com");
//下面的语句是不会执行的,因为页面已经跳转到了百度页面
alert(window.location.assign);
}
history对象
history.back() - 与在浏览器点击后退按钮相同
history.forward() - 与在浏览器中点击按钮向前相同
<html>
<head>
<script type="text/javascript">
function goBack(){
window.history.back();
}
function goFoward(){
window.history.forward();
}
</script>
</head>
<body>
<input type="button" value="goBack" onclick="goBack();">
<input type="button" value="goFoward" onclick="goFoward();">
</body>
</html>