JavaScript Window - 浏览器对象模型

JavaScript Window - 浏览器对象模型

  • 浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器“对话”。
  • 浏览器对象模型 (BOM)
  • 浏览器对象模型(Browser Object Model)尚无正式标准
  • 由于现代浏览器几乎实现了 JavaScript 交互性方面的相同方法和属性,因此常被认为是 BOM 的方法和属性

Window 对象

  • 所有浏览器都支持 window 对象。它表示浏览器窗口。
  • 所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员
  • 全局变量是 window 对象的属性
  • 全局函数是 window 对象的方法
  • 甚至 HTML DOM 的 document 也是 window 对象的属性之一

    window.document.getElementById("header")
    等价
    document.getElementById("header")
    

Window 尺寸

    有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)
    对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari
        window.innerHeight - 浏览器窗口的内部高度
        window.innerWidth - 浏览器窗口的内部宽度
    对于 Internet Explorer 8、7、6、5
        document.documentElement.clientHeight
        document.documentElement.clientWidth
        或者
        document.body.clientHeight
        document.body.clientWidth
    实例
    var w=window.innerWidth
    || document.documentElement.clientWidth
    || document.body.clientWidth;

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

    其他 Window 方法
        window.open() - 打开新窗口
        window.close() - 关闭当前窗口
        window.moveTo() - 移动当前窗口
        window.resizeTo() - 调整当前窗口的尺寸

JavaScript Window Screen

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

Window Screen

        window.screen 对象在编写时可以不使用 window 这个前缀
        screen.availWidth - 可用的屏幕宽度
        screen.availHeight - 可用的屏幕高度

Window Screen 可用宽度

        screen.availWidth 属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如窗口任务栏
        <script>
            document.write("可用宽度:" + screen.availWidth);
        </script>

Window Screen 可用高度

        screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。
        <script>
            document.write("可用高度:" + screen.availHeight);
        </script>

JavaScript Window Location

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面

Window Location

        window.location 对象在编写时可不使用 window 这个前缀
        location.hostname 返回 web 主机的域名
        location.pathname 返回当前页面的路径和文件名
        location.port 返回 web 主机的端口 (80 或 443)
        location.protocol 返回所使用的 web 协议(http:// 或 https://)

Window Location Href

        location.href 属性返回当前页面的 URL
        <script>
            document.write(location.href); // http://www.w3school.com.cn/js/js_window_location.asp
        </script>

Window Location Pathname

        location.pathname 属性返回 URL 的路径名
        <script>
            document.write(location.pathname); // /js/js_window_location.asp
        </script>

Window Location Assign

        location.assign() 方法加载新的文档
        <script>
            function newDoc() {
                window.location.assign("http://www.w3school.com.cn")
            }
        </script>

JavaScript Window History

        window.history 对象包含浏览器的历史
Window History
            window.history 对象在编写时可不使用 window 这个前缀
            为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制
            history.back() - 与在浏览器点击后退按钮相同
            history.forward() - 与在浏览器中点击按钮向前相同
Window History Back
            <script>
                function goBack() {
                    window.history.back()
                }
            </script>
Window History Forward
            <script>
                function goForward() {
                    window.history.forward()
                }
            </script>

JavaScript Window Navigator

        window.navigator 对象包含有关访问者浏览器的信息
        window.navigator 对象在编写时可不使用 window 这个前缀
            <div id="example"></div>

            <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>
            来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
            navigator 数据可被浏览器使用者更改
            浏览器无法报告晚于浏览器发布的新操作系统
        浏览器检测
            由于 navigator 可误导浏览器检测,使用对象检测可用来嗅探不同的浏览器。
            由于不同的浏览器支持不同的对象,您可以使用对象来检测浏览器。例如,由于只有 Opera 支持属性 "window.opera",您可以据此识别出 Opera。
            例子:if (window.opera) {...some action...}

JavaScript 消息框

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

警告框

        <head>
            <script>
                function disp_alert() {
                    alert("我是警告框!!")
                }
            </script>
        </head>
        <body>

            <input type="button" onclick="disp_alert()" value="显示警告框" />

        </body>

确认框

        <head>
            <script>
            function show_confirm() {
                var r=confirm("Press a button!");
                if (r==true) {
                    alert("You pressed OK!");
                } else {
                    alert("You pressed Cancel!");
                }
            }
            </script>
        </head>
        <body>
            <input type="button" onclick="show_confirm()" value="Show a confirm box" />
        </body>

提示框

        <head>
            <script type="text/javascript">
                function disp_prompt() {
                    var name=prompt("请输入您的名字","Bill Gates")
                    if (name!=null && name!="") {
                        document.write("你好!" + name + " 今天过得怎么样?")
                    }
                }
            </script>
        </head>
        <body>
            <input type="button" onclick="disp_prompt()" value="显示提示框" />
        </body>

JavaScript 计时

    通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件

单击本例中的按钮后,会在 5 秒后弹出一个警告框

        <script>
            function timedMsg() {
                var t=setTimeout("alert('5 秒!')",5000)
            }
        </script>

        <head>
            <script>
                function timedText() {
                    var t1=setTimeout("document.getElementById('txt').value='2 秒'",2000)
                    var t2=setTimeout("document.getElementById('txt').value='4 秒'",4000)
                    var t3=setTimeout("document.getElementById('txt').value='6 秒'",6000)
                }
            </script>
        </head>

本例中的程序会执行 2 秒、4 秒和 6 秒的计时

        <body>

            <form>
                <input type="button" value="显示计时的文本" onClick="timedText()">
                <input type="text" id="txt">
            </form>

            <p>点击上面的按钮。输入框会显示出已经逝去的时间(2、4、6 秒)。</p>
        </body>

单击开始计时按钮后,程序开始从 0 以秒计时

        <head>
            <script>
                var c=0
                var t
                function timedCount()
                {
                document.getElementById('txt').value=c
                c=c+1
                t=setTimeout("timedCount()",1000)
                }
            </script>
        </head>

        <body>

        <form>
            <input type="button" value="开始计时!" onClick="timedCount()">
            <input type="text" id="txt">
        </form>

        <p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p>

        </body>

点击计数按钮后根据用户输入的数值开始倒计时,点击停止按钮停止计时

        <head>
            <script>
                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>

使用计时事件制作的钟表

        <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>
    JavaScript 计时事件
        setTimeout()
        clearTimeout()

JavaScript Cookies

    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>

什么是cookie?

    cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。
    当访问者首次访问页面时,他或她也许会填写他/她们的密码。密码也可被存储于 cookie 中。当他们再次访问网站时,密码就会从 cookie 中取回。
    当访问者首次访问你的网站时,当前的日期可存储于 cookie 中。当他们再次访问网站时,他们会收到类似这样的一条消息:"Your last visit was on Tuesday August 11, 2005!"。日期也是从 cookie 中取回的。
    在这个例子中我们要创建一个存储访问者名字的 cookie。当访问者首次访问网站时,他们会被要求填写姓名。名字会存储于 cookie 中。当访问者再次访问网站时,他们就会收到欢迎词。

JavaScript 库

JavaScript 库 - jQuery、Prototype、MooTools

JavaScript 框架(库)

    JavaScript 高级程序设计(特别是对浏览器差异的复杂处理),通常很困难也很耗时。
    为了应对这些调整,许多的 JavaScript (helper) 库应运而生。
    这些 JavaScript 库常被称为 JavaScript 框架。
    在本教程中,我们将了解到一些广受欢迎的 JavaScript 框架:
        jQuery
        Prototype
        MooTools
    所有这些框架都提供针对常见 JavaScript 任务的函数,包括动画、DOM 操作以及 Ajax 处理。

CDN - 内容分发网络

    您总是希望网页可以尽可能地快。您希望页面的容量尽可能地小,同时您希望浏览器尽可能多地进行缓存。
    如果许多不同的网站使用相同的 JavaScript 框架,那么把框架库存放在一个通用的位置供每个网页分享就变得很有意义了。
    CDN (Content Delivery Network) 解决了这个问题。CDN 是包含可分享代码库的服务器网络。
    Google 为一系列 JavaScript 库提供了免费的 CDN,包括:
        jQuery
        Prototype
        MooTools
        Dojo
        Yahoo! YUI
    如需在您的网页中使用 JavaScript 框架库,只需在 <script> 标签中引用该库即可:
    引用 jQuery
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>

JavaScript - 测试 jQuery

    <!DOCTYPE html>
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
            </script>
            <script>
            function myFunction() {
                $("#h01").html("Hello jQuery")
            }
            $(document).ready(myFunction);
            // 等价于原生JavaScript
            // function myFunction() 
                // var obj=document.getElementById("h01");
                // obj.innerHTML="Hello jQuery";
            // }
            // onload=myFunction;
            </script>
        </head>

        <body>
            <h1 id="h01"></h1>
        </body>
    </html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值