BOM : Browser Object Model 浏览器对象模型。
其实我们经常写的window对象就是浏览器的一个对象,下面通过一些例子了解BOM常见的属性和方法。
1.窗口的打开(open())和关闭(close()):
这两个方法有兼容性的问题,在不同的浏览器下表现不同
例一:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function() {
var aInput = document.getElementsByTagName('input');
var opener = null;
aInput[0].onclick = function() {
//open(地址默认是空白页面,打开方式默认新窗口) 打开一个新窗口
//window.open('http://www.baidu.com', '_self');
opener = window.open();//返回值 返回的新开页面的window对象
//alert(opener == window)
//opener.document.body.style.background = 'red';
}
aInput[1].onclick = function() {
window.close();
/*
ff : 无法关闭
chrome : 直接关闭
ie : 询问用户
*/
}
aInput[2].onclick = function() {
opener.close(); //可以通过关闭用window.open方法打开的窗口
}
}
</script>
</head>
<body>
<input type="button" value="打开新窗口" />
<input type="button" value="关闭窗口" />
<input type="button" value="关闭新窗口" />
</body>
</html>
2.浏览器信息window.navigator.userAgent:
window.navigator.userAgent会返回浏览器的一些基本信息:包括浏览器的内核,版本等,我们可以利用这些信息对不同的浏览器做不同的事情。
例二:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
//window.navigator.userAgent : 浏览器信息
alert( window.navigator.userAgent )
if ( window.navigator.userAgent.indexOf('MSIE') != -1 ) {
alert('我是ie');
} else {
alert('我不是ie');
}
</script>
</head>
<body>
</body>
</html>
3.浏览器地址栏信息:
window.location : 地址
window.location.href = window.location内容
window.location.search = url?后面的内容
window.location.hash = url#后面的内容