首页 > 技术文章 > JavaScript对象(窗口对象 定时器对象 )

fengdashen 2016-01-09 19:44 原文

1:窗口时间

confirm(str):确认对话框,确认返回trun,取消返回false,但是必须要有两个return;不然就算按下取消也会提交

第一个return:用于保证确认按钮运行

<script>
    function update(){
        var r = confirm("go on updating?");
        return r; 
        }
</script>

第二个return:用于保证取消按钮也能运行

<form>
    <input type="text" name="user"/>
    <input type="submit" value="提交" onclick="return update()">
</form>

全部代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>window对象</title>
<script>
    function update(){
        var r = confirm("go on updating?");
        return r;
        
        }
</script>
</head>

<body>
<form>
    <input type="text" name="user"/>
    <input type="submit" value="提交" onclick="return update()">
</form>
</body>
</html>
View Code

代码效果:

提交之后:最终保证confirm顺利执行

 

2:页面控制:window.open()控制打开页面

1:按钮控制打开多个

2:按钮控制打开一个,后面加上该网页的内容

<body>
<input type="button" value="可重复打开新的窗口" onclick="window.open('window对象.html')"/>
<input type="button" value="只能打开一个新的窗口" onclick="window.open('window对象.html','window对象')"/>
</body>

变成超链接的方式:

<a href="javascript:window.open('window对象.html','window对象')">windows对象</a>

 打开网页的三种方式:

1:替换当前页面:

2:重复打开同一个连接:

3:同一个链接只能打开一次:

<p><a href="window对象.html">替换当前页面</a></p>
<p><a href="window对象.html" target="_blank">打开另一个选项卡</a></p>
<p><a href="#"onclick="javascript:window.open('window对象.html','window对象')">只能打开一次</a></p>

 

定时器对象:

多用于网页动态时钟,制作倒计时,跑马灯效果

周期性时钟:页面的倒计时

一次性时钟:等5s后关闭页面

往网页里面写回东西:

var s = hour+'小时'+minute+'分钟'+second+'秒';
h1.innerHTML = s;//任意元素中间的

页面一打开就执行函数:

window.addEventListener('load',miaosha,false)

 秒杀:

var timer;
var miaosha = function(){
    var h1 = document.getElementById("time");
    var now= new Date();
    //距离6点开始抢购
    var hour = 19-now.getHours()-1;
    var minute=59- now.getMinutes();
    var second = 59- now.getSeconds();
    var s = hour+'小时'+minute+'分钟'+second+'秒';
    h1.innerHTML = s;//任意元素中间的正文
    
    }
window.addEventListener('load',timerStart,false)

function timerStart(){
    timer = window.setInterval(miaosha,1000);
}

 

在JavaScript中,===表示完全相等,需要判断一个变量未定义,必须给他加上===等号

function stopTimer(){
    if(timer===undefined)
         timer = window.setInterval(miaosha,1000);
    else
        timer=clearInterval(timer);

}

 页面5s后关闭:

var timere;
function timeFunc(){
    var h1= document.getElementById("h1");
    var n =parseInt(h1.innerHTML);
    h1.innerHTML = (n-1)+"s后自动关闭"
    }    
function startTimer(){
    timere = setInterval(timeFunc,1000);
    }
window.addEventListener('load',startTimer,false);

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="页面5秒关闭.js"></script>
<title>myAlert</title>
</head>

<body>
<h1 id="h1">5s后自动关闭</h1>
留在此页面,请点击
<a href="#">这里</a>
</body>
</html>

 

 

推荐阅读