BOM(Broswer Object Model)
凡是 window 的属性和方法,均可以省略“window.”
方法:
框窗
1.警告框
复制代码
1window.alert("msg");
2.确认框
复制代码
1window.confirm("msg");
3.询问框
复制代码
1window.prompt("msg","defaulvalue")
页面
1.打开一个窗口
复制代码
1window.open()
2.在子窗口中使用,表示父窗口的window对象
window.opener
复制代码
1
2window.opener.fatherSayHello(); 调用父窗口的方法 window.opener.a
3.关闭当前窗口
window.close()
复制代码
1
2window.close(); 关闭当前 window.opener.close(); 关闭父窗口
定时任务
1.定时任务
复制代码
1
2var taskid = window.setTimeout(function,ms); window.setTimeout("alert('hello!')", 5000);
2.间隔执行任务
复制代码
1var taskid = window.setInteval(function,ms);
3.清除定时任务
复制代码
1window.clearTimeout(taskid);
4.清除间隔执行任务
复制代码
1window.clearInteval(taskid);
打印屏幕
复制代码
1
2//长*宽 console.log(screen.width + "*" + screen.height)
后退
复制代码
1window.history.back();
前进
复制代码
1window.history.forward();
刷新
复制代码
1
2window.location.reload();//刷新 window.location.href = window.location.href;//刷新
Go 前进(x)步,后退(x)步,刷新(0),
复制代码
1window.history.go(x)
链接
复制代码
1window.location.href = http://www.baidu.com;
用户代理 浏览器内核
复制代码
1console.log(window.navigator.userAgent)
框窗
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28//凡是window的属性和方法,均可以省略“window.” <script type="application/javascript"> // 警告框 function testAlert(){ var result=window.alert("这是一个警告框") console.log(result); } // confirm 确认框 function testConfirm(){ var result =window.confirm("你确认要离开了吗?"); if(result){ alert("欢迎下次再来!") }else{ alert("那你在逛逛吧!") } consol.log(result); } // prompt 询问框 function testPrompt(){ var result = window.prompt("请输入U盾密码","例如:123456"); console.log(result); } </script> <body> <button onclick="testAlert();">testAlert</button> <button onclick="testConfirm();">testConfirm</button> <button onclick="testPrompt();">testPrompt</button> </body>
页面
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44//子页面 <script type="application/javascript"> function sayHello(){ alert("hello world") } //打开一个窗口 function callFatherMethod(){ window.opener.fatherSayHello(); window.opener.a } //关闭本窗口 function testClose(){ window.close(); } //关闭父窗口 function testFatherClose(){ window.opener.close(); } </script> <body> <button onclick="callFatherMethod()">调用父窗口的方法</button> <button onclick="testClose()">关闭本窗口</button> <button onclick="testFatherClose()">关闭父窗口</button> </body> //父页面 <script type="application/javascript"> var a = 10; window.onload = function(){ console.log(window); console.log("11111111111") } //打开一个新窗口 function testOpen(){ var sonwindow = window.open("son.html","aaa","height=300,width=500,top=50,left=50,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no") //子窗口的window对象 console.log(sonwindow); } function fatherSayHello(){ alert("hello son!"); } </script> <body> <button onclick="testOpen();">打开一个新窗口</button> </body>
定时任务
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13<script type="application/javascript"> function setTime() { // window.setTimeout("alert('hello!')",5000); window.setTimeout(sayHello, 5000); } var sayHello = function () { alert("你好!"); } </script> </head> <body> <button onclick="sayHello();">调用sayHello</button> <button onclick="setTime();">调用setTime</button>
间隔执行任务
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48<script type="application/javascript"> /* window.onload = function(){ window.setTimeout(closeSelf, 1000); } function closeSelf() { var secval = document.getElementById("sec").innerHTML; var secint = parseInt(secval); document.getElementById("sec").innerHTML = --secint; if(secint == 0){ window.close(); } window.setTimeout(closeSelf, 1000); } */ var taskid = 0; window.onload = function(){ //间隔执行任务,间隔 1000ms 执行一次 taskid = window.setInterval(closeSelf, 1000); } function closeSelf() { //获取 10s var secval = document.getElementById("sec").innerHTML; console.log(secval); var secint = parseInt(secval); console.log(secint); //10s 减减 document.getElementById("sec").innerHTML = --secint; if(secint == 0){ window.close(); } } // 4.清除间隔执行任务 暂停 function stopTask(){ window.clearInterval(taskid); } //继续计时 function goonTask(){ taskid = window.setInterval(closeSelf, 1000); console.log(taskid) } </script> <body> 付款成功,页面将在<span id="sec">10</span>s后关闭。 <button onclick="stopTask()">稍等,待会我会自己关闭</button> <button onclick="goonTask()">继续读秒,关闭窗口</button> </body>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是缥缈航空最近收集整理的关于深入学习JavaScript中的bom的全部内容,更多相关深入学习JavaScript中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复