深入学习JavaScript中的bom
___mouM 人气:0BOM(Broswer Object Model)
凡是 window 的属性和方法,均可以省略“window.”
方法:
框窗
1.警告框
window.alert("msg");
2.确认框
window.confirm("msg");
3.询问框
window.prompt("msg","defaulvalue")
页面
1.打开一个窗口
window.open()
2.在子窗口中使用,表示父窗口的window对象
window.opener
window.opener.fatherSayHello(); 调用父窗口的方法 window.opener.a
3.关闭当前窗口
window.close()
window.close(); 关闭当前 window.opener.close(); 关闭父窗口
定时任务
1.定时任务
var taskid = window.setTimeout(function,ms); window.setTimeout("alert('hello!')", 5000);
2.间隔执行任务
var taskid = window.setInteval(function,ms);
3.清除定时任务
window.clearTimeout(taskid);
4.清除间隔执行任务
window.clearInteval(taskid);
打印屏幕
//长*宽 console.log(screen.width + "*" + screen.height)
后退
window.history.back();
前进
window.history.forward();
刷新
window.location.reload();//刷新 window.location.href = window.location.href;//刷新
Go 前进(x)步,后退(x)步,刷新(0),
window.history.go(x)
链接
window.location.href = http://www.baidu.com;
用户代理 浏览器内核
console.log(window.navigator.userAgent)
框窗
//凡是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>
页面
//子页面 <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>
定时任务
<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>
间隔执行任务
<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>
加载全部内容