window对象
window对象是BOM的核心,window对象指当前的浏览器窗口
function newWindow(){
var a=window.confirm("欢迎来到慕课网");
if(a){
window.open('http://www.imooc.com',
'_blank','height=200,width=200');
}
}
计时器类型: 一次性计时器:仅在指定的延迟时间之后触发一次。 间隔性触发计时器:每隔一定的时间间隔就触发一次。
setInterval(代码,交互时间);
1. 代码:要调用的函数或要执行的代码串。
2. 交互时间:周期性执行或调用表达式之间的时间间隔,以毫秒计(1s=1000ms)。
3. 返回值:
一个可以传递给 clearInterval() 从而取消对”代码”的周期性执行的值。
var attime;
function clock(){
var time=new Date();
attime=time.getMinutes()+':'+time.getSeconds();
document.getElementById("clock").value = attime;
}
setInterval(clock(),1000);
clearInterval() 方法可取消由 setInterval() 设置的交互时间。
function clock(){
var time=new Date();
document.getElementById("clock").value = time;
}
var interval=setInterval(clock,1000);
<input type="button" value="Stop" onclick="clearInterval(interval)" />
setTimeout()计时器,在载入后延迟指定时间后,去执行一次表达式,仅执行一次。
var num=0; function startCount() {
document.getElementById(‘count’).value=num;
num=num+1;
setTimeout(“startCount()”,1000); } setTimeout(“startCount()”,1000);在函数内使用setTimeout(),实现自身调用,创建一个循环计数器。
在函数外使用setTimeout(),在设定时间后执行startCount()函数。 取消计时器 i=setTimeout(“startCount()”,1000); clearTimeout(i);
window.history.length();
window.history.back();
window.history.forward();
window.history.go(number);
HTML5为history对象添加了两个新方法,
history.pushState()和history.replaceState(),用来在浏览历史中添加和修改记录。
Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。
document.write(navigator.appCodeName+"<br/>");//1-浏览器代码名的字符串表示
document.write(navigator.appName+'<br/>');//2-浏览器名称
document.write(navigator.appVersion+'<br/>');//3-浏览器平台和版本名称
document.write(navigator.platform+'<br/>');//4-运行浏览器的操作系统平台
document.write(navigator.userAgent+'<br/>');//5-由客户机发给服务器的user-agent头部
1->Mozilla
2->Netscape
3->5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36
4->Win32
5->Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36
> **Location对象**
> location用于获取或设置窗体的URL,并且可以用于解析URL
属性
//document.write(location.hash+’
’);//设置或返回从(#)开始的URL(锚)
document.write(location.host+’
’);//1设置或返回主机名和当前url的端口号
document.write(location.hostname+’
’);//2设置或返回当前url的主机名
document.write(location.href+’
’);//3设置或返回完整的url
document.write(location.pathname+’
’);//4设置或返回当前url上网路径部分
//document.write(location.port+’
’);//设置或返回当前url的端口号
document.write(location.protocol+’
’);//5设置或返回当前url上网协议。
//document.write(location.search+’
’);//设置或返回从问号开始的url
结果:
1->www.imooc.com
2->www.imooc.com
3->https://www.imooc.com/code/1153
4->/code/1153
5->https:
方法
location.reload();//加载新的文档
location.assign("https://www.baidu.com");//重新加载当前文档
location.replace("https://www.imooc.com/code/1153");//用新的文档替换当前文档
userAgent 返回用户代理头的字符串表示(就是包括浏览器版本信息等的字符串)
判断使用的浏览器
function validB(){
var u_agent =navigator.userAgent;
var B_name="不是想用的主流浏览器!";
if(u_agent.indexOf("Firefox")>-1){
B_name="Firefox";
}else if(u_agent.indexOf("Chrome")>-1){
B_name="Chrome";
}else if(u_agent.indexOf("MSIE")>-1&&u_agent.indexOf("Trident")>-1){
B_name="IE(8-10)";
}
document.write("浏览器:"+B_name+"<br>");
document.write("u_agent:"+u_agent+"<br>");
}
screen对象:用于获取用户的屏幕信息。
属性
availHeight:窗口可以使用的屏幕高度,单位像素;
availWidth:窗口可以使用的屏幕宽度,单位像素;
colorDepth:用户浏览器表示的颜色位数,通常为32位(每像素的位数);
pixelDepth:用户浏览器表示的颜色位数,通常为32位(每像素的位数)(IE不支持此属性);
height:屏幕的高度,单位像素;
width:屏幕的宽度,单位像素;