当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS 06 bom 框窗_页面_定时任务

JS 06 bom 框窗_页面_定时任务

2019年05月06日  | 移动技术网IT编程  | 我要评论
bom(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>

 

2.间隔执行任务
   <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>

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网