JavaScript_基础_window对象

Window对象方法:

1.计时器

setTimeout()指定延迟时间之后执行代码

clearTimeout()取消setTimeout()设置

setInterval()每隔指定时间执行代码***一个例子菜单栏制作

clearInterval()取消setInterval()设置

假如函数名为A():

var i=setInterval(A,100);

var i=setInterval("A()",100);这两种写法正确

关闭可用:onclock("clearInterval(i)")实现,也可加confirm():

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
   var a;
   function clock(){
      var time=new Date();                  
      document.getElementById("clock").value = time;     
   }   
    var i=setInterval("clock()",100); 
   function stoptimes(){
    if(confirm("?")){
      clearInterval(i);
    }
   }
</script>
</head>
<body>
  <form>
    <input type="text" id="clock" size="50"  />
    <input type="button" value="Stop" οnclick="stoptimes()" />
  </form>
</body>
</html>

setTimeout()与clearTimeout()的使用和Interval类似,只不过他们只执行一次。

2.history对象

window.history.length  返回浏览器历史列表中URL数量,即后退能退几次+1

window.history.back() 加载历史列表前一个URL,相当于go(-1)。

window.history.forward() 加载历史列表后一个URL,相当于go(1)。

window.history.go(num) 加载历史列表中某个具体页面,num为相对位置(参考上面)

3.Location对象

图片转自imooc


对象的属性有:

hash:设置或返回从#开始的URL

host:设置或返回主机名和当前URL端口

hostname:设置或返回当前URL的主机名

href:设置或返回完整的URL

pathname:设置或返回当前URL的路径部分

port:设置或返回当前URL的端口号

protocol:设置或返回当前URL的协议

search:设置或返回从问号(?)开始的URL查询部分

对象方法:

assign:加载新文档

reload:重载当前文档

replace:用新文档替换当前文档

4.Navigator对象

图片转自imooc

5.screen对象

window.screen.height


eg:一个练习,登陆成功后返回到百度主页,有倒计时的那种

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录到百度</title>
</head>
<script type="text/javascript">
  function loging(){
    var ac=document.getElementById("account").value;
    var pw=document.getElementById("password").value;
    var countdown=5;
    if(confirm("您确定要登陆吗?")){
      if(ac=="account"&&pw=="password"){
          var i=setInterval(function(){
              document.write("即将回到百度!倒计时:"+countdown+"秒"+"<br>");
              countdown=countdown-1;
              if(countdown==0){
                clearInterval(i);
                window.open("http://www.baidu.com","_blank","height=600,width=1000");
              }
          },200);
      }
    }

  }
 
</script>
<body>
用户名:
<input type="account" id="account" value="account">
<br>
密码:
<input type="password" id="password" value="password">
<br>
<input type="button" id="login" value="登录" οnclick="loging()">
</body>
</html>
如何在刷新页面后输出一个有链接的文本,实现立即返回,还有待学习。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值