JavaScript随记4

一. window 对象

1.打开一个新窗口: open() 和 关闭一个窗口:close()

      var winOpen;
    //打开一个新的窗口
    function winOpen(){
        
        winOpen = window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes')   
        
        }
        
    //关闭一个浏览器窗口
    function winClose(){
        
        winOpen.close()
        
        }
  1. 提示框: alert() prompt() confirm()
    //显示带有一段消息和一个确认按钮的警告框
    function winAlert(){
        window.alert("我只是一段测试文字");
        }
    //显示可提示用户输入的对话框
    function winPrompt(){
        window.prompt("请输入名字","这里输入密码");
        }
    //显示带有一段消息以及确认按钮和取消按钮的对话框
    function winConfirm(){
        window.confirm("请完善您的信息");
        }
  1. 开启和关闭计时器:1. setInterval() clearInterval() ------ 2.setTimeout() clearTimeout()
    第一种方法:  
    var interval;
    //开启一个计时器setInterval() 
    function openInterval(){
        interval = setInterval("clock()",100);
        }   
    //关闭计时器
    function closeInterval(){
        clearInterval(interval);
        alert("计时器关闭!!");
        }

    第二种方法
        var time;
    //开始计时器
    function openTimeout(){
            document.getElementById("con1").value = num;
            document.getElementById("con").disabled = true;
            if(num == 0){
                document.getElementById("con").disabled = false;
                num = 10;
            }else{  
                time = setTimeout("openTimeout()",1000);    
            }
            num = num + 1;
        }
        
      //取消计时器
      function closeTimeout(){
        clearTimeout(time);
        document.getElementById("con").disabled = false;
        }
  1. History 对象
     document.write(window.history.length + "<br>");  //返回浏览器中历史列表中的URL数量
    
    //加载上一个页面
    window.history.back();
    
    //加载下一个页面
    window.history.forward();
    
    //go()方法,根据当前所处的页面,加载 history 列表中的某个具体的页面。
    window.history.go(0);

5.Location 对象

    document.write("href: " + window.location.href + "<br>");  // 完整的URL地址
    
    document.write("hash: " + window.location.hash + "<br>"); // 从#号开始的URL(锚)
    
    document.write("host: " + window.location.host + "<br>");  //主机名或当前URL的端口号
    
    document.write("hostname: " + window.location.hostname + "<br>");  //当前URL的主机名
    
    document.write("pathname: " + window.location.pathname + "<br>");  //当前URL的路径部分
    
    document.write("port: " + window.location.port + "<br>"); // 当前URL的端口号
    
    document.write("protocol: " + window.location.protocol + "<br>");  //当前URL的协议
    
    document.write("search: " + window.location.search + "<br>"); 从?号开始的URL(查询部分)

6.Navigator 对象

document.write("appCodeName: " + window.navigator.appCodeName + "<br>");  //浏览器代码名的字符串表示
    
    document.write("appName: " + window.navigator.appName + "<br>");  //返回浏览器名称
    
    document.write("appVersion: " + window.navigator.appVersion + "<br>");  //返回浏览器的平台和版本信息
    
    document.write("platform: " + window.navigator.platform + "<br>");  //返回运行浏览器的操作系统平台
    
    document.write("userAgent: " + window.navigator.userAgent + "<br>");  // 返回由客户机发送服务器的user-agent头部的值

7.screen 对象

document.write("availHeight: " + window.screen.availHeight + "<br>");  // 窗口可以使用的屏幕高度,单位像素

document.write("availWidth: " + window.screen.availWidth + "<br>");   //窗口可以使用的屏幕宽度,单位像素

document.write("colorDepth: " + window.screen.colorDepth + "<br>");  //用户浏览器表示的颜色位数,通常为 32位

document.write("pixelDepth: " + window.screen.pixelDepth + "<br>");  //用户浏览器表示的颜色位数,通常为 32位

document.write("height: " + window.screen.height + "<br>");   //屏幕的高度

document.write("width: " + window.screen.width + "<br>");   //屏幕的宽度
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、JS前言 (1)认识JS 也许你已经了解HTML标记(也称为结构),知道了CSS样式(也称为表示),会使用HT...
    凛0_0阅读 2,798评论 0 8
  • ECMAScript 是 JavaScript 的核心,但如果要在 Web 中使用 JavaScript,那么 B...
    劼哥stone阅读 840评论 2 5
  • JS Window-浏览器对象模型 浏览器对象模型(BOM)使JS有能力与浏览器对话 由于现代浏览器几乎实现了JS...
    figure_ai阅读 1,296评论 0 2
  • 一、JS定义及应用 JavaScript:一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型...
    yzw12138阅读 447评论 0 0
  • 第1章 认识JS JavaScript能做什么?1.增强页面动态效果(如:下拉菜单、图片轮播、信息滚动等)2.实现...
    mo默22阅读 1,337评论 0 5