JavaScript_基础_window对象

来源:互联网 发布:网络远程教育大学 编辑:程序博客网 时间:2024/06/05 03:50

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" onclick="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="登录" onclick="loging()"></body></html>
如何在刷新页面后输出一个有链接的文本,实现立即返回,还有待学习。



0 0
原创粉丝点击