• javascript的window操作


    注意:此文档是对于w3school的学习与整理

    BOM浏览器对象模型(Browser Object Model)

    1. 获取window的尺寸

    var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    
    var h=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    

    2.  window.open() - 打开新窗口, 

    3. window.close() - 关闭当前窗口,  

    4. window.moveTo() - 移动当前窗口, 

    5. window.resizeTo() - 调整当前窗口的尺寸

    6. window.screen 对象包含有关用户屏幕的信息。

    <script>
    document.write("可用宽度:" + screen.availWidth);
    document.write("可用高度:" + screen.availHeight);
    </script>
    

    7. window.location 对象在编写时可不使用 window 这个前缀。

      location.hostname 返回 web 主机的域名
      location.pathname 返回当前页面的路径和文件名
      location.port 返回 web 主机的端口 (80 或 443)
      location.protocol 返回所使用的 web 协议(http:// 或 https://)
      location.href 属性返回当前页面的 URL
      location.assign(url) 方法加载新的文档

    8. window.history 对象在编写时可不使用 window 这个前缀。

      history.back() - 与在浏览器点击后退按钮相同
      history.forward() - 与在浏览器中点击按钮向前相同

    9. window.navigator 对象包含有关访问者浏览器的信息。window.navigator 对象在编写时可不使用 window 这个前缀。navigator 数据可被浏览器使用者更改.由于只有 Opera 支持属性 "window.opera",您可以据此识别出 Opera。

    <script>
    txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
    txt+= "<p>Browser Name: " + navigator.appName + "</p>";
    txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
    txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
    txt+= "<p>Platform: " + navigator.platform + "</p>";
    txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
    txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";
    document.getElementById("example").innerHTML=txt;
    </script>
    

    10. 可以在 JavaScript 中创建三种消息框:警告框、确认框、提示框。

      警告框: alert("文本")
      确认框: confirm("文本"); 如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。
      提示框: prompt("文本","默认值"); 如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。

    11. js实现计时

    开始, 关闭功能

    <html>
    <head>
    <script type="text/javascript">
    var c = 0;
    var t;
    function timedCount()
    {
      document.getElementById('txt').value = c;
      c = c + 1;
      t = setTimeout("timedCount()", 1000);
    }
    
    function stopCount()
    {
      c = 0;
      setTimeout("document.getElementById('txt').value=0", 0);
      clearTimeout(t);
    }
    </script>
    </head>
    <body>
    <form>
      <input type="button" value="开始计时!" onClick="timedCount()">
      <input type="text" id="txt">
      <input type="button" value="停止计时!" onClick="stopCount()">
    </form>
    <p>请点击上面的“开始计时”按钮来启动计时器。输入框会一直进行计时,从 0 开始。点击“停止计时”按钮可以终止计时,并将计数重置为 0。</p>
    </body>
    </html>
    

    实时计时

    <html>
    <head>
    <script type="text/javascript">
    function startTime()
    {
      var today=new Date();
      var h=today.getHours();
      var m=today.getMinutes();
      var s=today.getSeconds();
      // add a zero in front of numbers<10
      m=checkTime(m);
      s=checkTime(s);
      document.getElementById('txt').innerHTML=h+":"+m+":"+s;
      t=setTimeout('startTime()', 500);
    }
    
    function checkTime(i)
    {
      if (i < 10) {
        i = "0" + i;
      }
      return i;
    }
    </script>
    </head>
    
    <body onload="startTime()">
    <div id="txt"></div>
    </body>
    </html>
    

    12. cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。

    <html>
    <head>
    <script type="text/javascript">
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
    {
      c_start=document.cookie.indexOf(c_name + "=")
      if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1 
        c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1) c_end=document.cookie.length
        return unescape(document.cookie.substring(c_start,c_end))
        } 
      }
      return ""
    }
    
    function setCookie(c_name,value,expiredays)
    {
      var exdate=new Date()
      exdate.setDate(exdate.getDate()+expiredays)
      document.cookie=c_name+ "=" +escape(value)+
      ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    }
    
    function checkCookie()
    {
      username=getCookie('username')
      if (username!=null && username!="") {
        alert('Welcome again '+username+'!')
      }
      else {
        username=prompt('Please enter your name:',"")
        if (username!=null && username!="")
        {
          setCookie('username',username,365)
        }
      }
    }
    </script>
    </head>
    <body onLoad="checkCookie()">
    </body>
    </html>
    

      

      

      

      

      

  • 相关阅读:
    你读了该博客中哪些超链接?有何感想
    最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助?
    1500802028 王莉娟
    解码方法
    N皇后问题
    两个链表的交叉
    全排列
    交叉字符串
    翻转链表
    爬楼梯
  • 原文地址:https://www.cnblogs.com/floraCnblogs/p/js-window-option.html
Copyright © 2020-2023  润新知