美文网首页
JavaScript随记4

JavaScript随记4

作者: 梦之志 | 来源:发表于2016-11-23 15:50 被阅读0次

    一. 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>");   //屏幕的宽度
    

    相关文章

      网友评论

          本文标题:JavaScript随记4

          本文链接:https://www.haomeiwen.com/subject/gndvpttx.html