JavaScript中的BOM对象
浏览器对象模型--Browser Object Model (BOM)
主要的对象属于window对象中的内容。
window对象我们不需要手动创建,是一个内置的对象,我们只管使用。
window对象中的属性
确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)
window对象中的函数
open() | 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口 |
close() | 方法用于关闭浏览器窗口。 |
open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口
格式:window.open(URL,name,features,replace)
参数1-URL:要在新窗口中显示的文档的地址[可选的字符串].
没有url[1.不写 2.空字符串]---窗口依然打开,只是没有内容
参数2--name:新窗口的名称.
可以用作标记 <a> 和 <form> 的属性 target 的值.
如果该参数指定了一个已经存在的窗口,那么 open() 方法就不再创建一个窗口,而只是返回对指定窗口的引用。在这种情况下,features 将被忽略。
参数3-features:新窗口要显示的标准浏览器的特征【尺寸】
如果省略该参数,新窗口将具有所有标准特征。
参数4-replace:规定了装载到窗口的 URL 是在窗口的浏览历史中创建一个新条目,还是替换浏览历史中的当前条目.
true - URL 替换浏览历史中的当前条目。
false - URL 在浏览历史中创建新的条目。
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var butdom1=document.getElementById("but1");
butdom1.onclick=function(){
//window.open(); //about:blank--空白页
//window.open("test.html","mytest","width:200,height:200",false);
//window.open("https://www.baidu.com/","width:200,height:100",false)
window.open("about:blank",false);
}
}
</script>
</head>
<body>
<input id="but1" type="button" value="测试open函数,打开新窗口"/>
</body>
</html>
close() 方法用于关闭浏览器窗口。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var butdom2=document.getElementById("but2");
butdom2.onclick=function(){
window.close();
};
}
</script>
</head>
<body>
<input id="but2" type="button" value="测试close函数,关闭窗口"/>
</body>
</html>
关于弹框的函数
警告框 | window.alert("sometext"); |
确认框 | window.confirm("sometext"); |
提示框 | window.prompt("sometext","defaultvalue"); |
关于弹框的函数
警告框:window.alert("sometext");
确认框:window.confirm("sometext");
当确认卡弹出时,用户可以点击 "确认" 或者 "取消" 来确定用户操作。
当你点击 "确认", 确认框返回 true, 如果点击 "取消", 确认框返回 false。
提示框:window.prompt("sometext","defaultvalue");
当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
如果用户点击确认,那么返回值为输入的值.
如果用户点击取消,那么返回值为 null。
参数1---提示信息
参数2---提示框的默认值
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var butdom1=document.getElementById("but1");
butdom1.onclick=function(){
//window.alert("测试警告框"); //标准写法
alert("测试警告框"); //简化写法
};
var butdom2=document.getElementById("but2");
butdom2.onclick=function(){
var flag=window.confirm("确定要退出吗?");
if(flag){
window.close();
}
};
var butdom3=document.getElementById("but3");
butdom3.onclick=function(){
var returnvalue=window.prompt("请输入姓名","admin");
//点击取消,返回null;
if(returnvalue==null){
alert("点击了取消");
}else{
//点击确定,得到输入结果
if(returnvalue.length==0){
alert("空内容");
}else{
alert("输入的数据是="+returnvalue+",类型是=="+typeof returnvalue);
}
}
};
}
</script>
</head>
<body>
<input id="but1" type="button" value="测试警告框"/><br>
<input id="but2" type="button" value="测试确认框"/><br>
<input id="but3" type="button" value="测试提示框"/>
</body>
</html>
window对象中的子对象
screen | 屏幕对象 |
location | 页面的地址对象 |
history | 历史对象 |
navigator | 浏览器信息对象 |
window.screen 对象包含有关用户屏幕的信息。
- 总宽度和总高度 --- width / height
- 可用宽度和可用高度----availWidth / availHeight
- 色彩深度----colorDepth
- 色彩分辨率---pixelDepth
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
//得到屏幕的总宽度和总高度
var screen_width=window.screen.width;
var screen_height=window.screen.height;
//可用宽度和可用高度----availWidth / availHeight
var availWidth=window.screen.availWidth;
var availHeight=window.screen.availHeight;
//3.色彩深度----colorDepth
var colorDepth=window.screen.colorDepth;
//4.色彩分辨率---pixelDepth
var pixelDepth=window.screen.pixelDepth;
document.write("<h1>总宽度和总高度=="+screen_width+"*"+screen_height+"</h1>");
document.write("<h1>可用宽度和可用高度=="+availWidth+"*"+availHeight+"</h1>");
document.write("<h1>色彩深度=="+colorDepth+"</h1>");
document.write("<h1>色彩分辨率=="+pixelDepth+"</h1>");
}
</script>
</head>
<body>
</body>
</html>
window对象中的子对象--[location--页面的地址对象]
href 属性返回当前页面的 URL。
search 属性是一个可读可写的字符串,可设置或返回当前 URL 的查询部分(问号 ? 之后的部分)。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var textdom=document.getElementById("text1");
var passdom=document.getElementById("pass1");
var butdom=document.getElementById("but1");
butdom.onclick=function(){
var zhanghao=textdom.value;
var passvalue=passdom.value;
if(zhanghao.length==0 || passvalue==0){
alert("账号密码不能为空!");
}else{
//href 属性返回当前页面的 URL。
//alert(window.location.href);
//window.location.href="success.html";
//window.location.href="success.html?zhanghao="+zhanghao;
window.location.href="success.html?zhanghao="+zhanghao+"&password="+passvalue;
}
};
}
</script>
</head>
<body>
<center>
<table border="1px">
<tr align="center">
<td colspan="2"><h1>用户登录</h1></td>
</tr>
<tr align="center">
<td>账号:</td>
<td><input id="text1" type="text" value="账号"></td>
</tr>
<tr align="center">
<td>密码:</td>
<td><input id="pass1" type="password" value=""></td>
</tr>
<tr align="center">
<td colspan="2"><input id="but1" type="button" value="登录"></td>
</tr>
</table>
</center>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
//search 属性是一个可读可写的字符串,可设置或返回当前 URL 的查询部分(问号 ? 之后的部分)。
//alert(window.location.search); //?zhanghao=qqqq&password=123456
var querystr=window.location.search;
var name=null;
var pass=null;
if(querystr.length==0){
window.location.href="javascript5.html";
}else{
//?zhanghao=qqqq
//var val=querystr.substring(querystr.lastIndexOf("=")+1,querystr.length);
//var h1array=document.getElementsByTagName("h1");
//h1array[0].innerHTML=val+",登录成功!";
//?zhanghao=qqqq&password=123456
var strarray=querystr.split("&");
//strarray[0]---?zhanghao=qqqq
//strarray[1]---password=123456
for(var i=0;i<strarray.length;i++){
var infoarray=strarray[i].split("=");
if(infoarray[0]=="?zhanghao"){
name=infoarray[1];
}
if(infoarray[0]=="password"){
pass=infoarray[1];
}
}
var h1array=document.getElementsByTagName("h1");
h1array[0].innerHTML=name+","+pass+",登录成功!";
}
}
</script>
</head>
<body>
<center>
<h1>登录成功!</h1>
</center>
</body>
</html>
window对象中的属性
确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)
有三种方法
1.对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari使用
window.innerHeight - 浏览器窗口的内部高度
window.innerWidth - 浏览器窗口的内部宽度
2.对于 Internet Explorer 8、7、6、5使用
document.documentElement.clientHeight - 浏览器窗口的内部高度
document.documentElement.clientWidth - 浏览器窗口的内部宽度
或者
document.body.clientHeight - 浏览器窗口的内部高度
document.body.clientWidth - 浏览器窗口的内部宽度
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>浏览器窗口的尺寸</title>
<script>
window.onload=function(){
//1.对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari使用
//var height=window.innerHeight;
//var width=window.innerWidth;
//document.write("<h1>"+height+"*"+width+"</h1>");
//2.对于 Internet Explorer 8、7、6、5使用
//var height=document.documentElement.clientHeight;
//var width=document.documentElement.clientWidth;
//var height=document.body.clientHeight;
//var width=document.body.clientWidth;
//document.write("<h1>"+height+"*"+width+"</h1>");
var width=window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth;
var height=window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight;
document.write("<h1>"+height+"*"+width+"</h1>");
}
</script>
</head>
<body>
</body>
</html>
window对象中的子对象--[history--历史对象]
back() - 与在浏览器点击后退按钮相同
forward() - 与在浏览器中点击按钮向前相同
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var butdom1=document.getElementById("but1");
butdom1.onclick=function(){
//forward() - 与在浏览器中点击按钮向前相同
window.history.forward();
}
}
</script>
</head>
<body>
<h1><a href="test2.html">打开test2</a></h1>
<input id="but1" type="button" value="向前-->"/>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var butdom1=document.getElementById("but1");
butdom1.onclick=function(){
//forward() - 与在浏览器中点击按钮向前相同
window.history.forward();
};
var butdom2=document.getElementById("but2");
butdom2.onclick=function(){
//back() - 与在浏览器点击后退按钮相同
window.history.back();
}
}
</script>
</head>
<body>
<h1><a href="test3.html">打开test3</a></h1>
<input id="but1" type="button" value="向前-->"/>
<input id="but2" type="button" value="向后 <--"/>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var butdom1=document.getElementById("but1");
butdom1.onclick=function(){
//back() - 与在浏览器点击后退按钮相同
window.history.back();
};
}
</script>
</head>
<body>
<input id="but1" type="button" value="向后 <--"/>
</body>
</html>
window对象中的子对象--[navigator--浏览器的信息对象]
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
document.write("<h1>浏览器代号:"+window.navigator.appCodeName+"</h1>");
document.write("<h1>浏览器名称:"+window.navigator.appName+"</h1>");
document.write("<h1>浏览器版本:"+window.navigator.appVersion+"</h1>");
document.write("<h1>启用Cookies:"+window.navigator.cookieEnabled+"</h1>");
document.write("<h1>硬件平台:"+window.navigator.platform+"</h1>");
document.write("<h1>用户代理:"+window.navigator.userAgent+"</h1>");
document.write("<h1>用户代理语言:"+window.navigator.systemLanguage+"</h1>");
</script>
</head>
<body>
</body>
</html>