第三部分:window对象

window对象:表示浏览器中打开的窗口,提供关于窗口状态的信息。可以用window对象访问窗口中绘制的文档、窗口中发生的事件和影响窗口的浏览器特性。

1.open()方法

(1)基本语法

                   window.open(url,[name],[features]);

        

解释:①url:表示要打开的窗口链接地址

②name:表示描述被打开的窗口的名称,可以是window.name也可以使”_blank”,”_top”, 这里的名称跟“<a href="..." target="...">”里的“target”属性是一样的。

③features:表示描述被打开的窗口的样貌。

例如打开一200*100的url为demo.htm的新窗体

window.open('demo.htm','_blank','width=200,height=100,menubar=no,toolbar=no,

location=no,directories=no,status=no, scrollbars=yes,resizable=yes')

top=# 窗口顶部离开屏幕顶部的像素数

left=# 窗口左端离开屏幕左端的像素数

width=# 窗口的宽度

height=# 窗口的高度

menubar=... 窗口有没有菜单,取值yes或no

toolbar=... 窗口有没有工具条,取值yes或no

location=... 窗口有没有地址栏,取值yes或no

directories=... 窗口有没有连接区,取值yes或no

scrollbars=... 窗口有没有滚动条,取值yes或no

status=... 窗口有没有状态栏,取值yes或no

resizable=... 窗口给不给调整大小,取值yes或no

2.opener属性

                   即获得打开本窗体的父窗体,仍是window对象

3.close()方法

                   即关闭窗体的方法。

4. showModalDialog()方法

                   即弹出模式对话框,使浏览器的其他窗体不能使用。

5. alert(),confirm(),prompt()方法

(1)alert()方法

即弹出一个提示或者警告,让用户知道信息。如:alert(“出错了!!”)

(2)confirm()方法

即弹出一个消息,确定或者取消,最后返回一个bool值。

(3)prompt()方法

         即显示带消息和输入字段的提示对话框,当点击确定时返回输入字段中的消息,点击取消时返回null。

6. location对象

窗口的location属性引用的是Location对象,它代表该窗口(或帧)中当前显示的文档的URL。Location对象的href属性是一个字符串,它包含URL的完整文本。Location对象的toString()方法返回href属性的值,因此,可以使用location代替location.href。

(1)window.location.protocol:设置或获取URL的协议部分。

               如当窗口的href为http://www.baidu.com时,该属性显示“http:”。

(2)window.location.host:设置或获取location或URL的hostname和port号码

          如当窗口的href为http://localhost:1859/Test.htm时,该属性显示“localhost:1859”。

(3)window.location.pathname:设置或获取对象指定的文件名或路径。

          如当窗口的href为http://localhost:1859/Test.htm时,该属性显示为“/Test.htm”。

(4)window.location.search:设置或者获取href属性中跟在问号后面的部分(包含问号)

          例如当窗口的href为http://localhost:1859/Test.htm?a=123&b=456时,该属性显示为“?a=123&b=456”。

7. history对象

     属性:length——获取历史列表中的元素数目

     方法:

          back();  从历史列表中装入前一个URL

          forward();从历史列表中装入下一个URL

          go();从历史列表中装入URL。例如history.go(-1)相当于后退。

8. 计时器(注意要执行的函数必须是全局的)

(1)函数:setInterval(函数名,时间);——在间隔的时间里重复调用函数,注意函数要用双引号引起来。返回一个不确定的值,这个值可以传递给clearInterval()来取消规划的函数

例如:

    <script type="text/javascript">

        function A() {

            alert("123");

        }

      window.setInterval("A();", 5000);

    </script>

(2)函数:clearInterval(id);——关闭Interval。

例如:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script type="text/javascript">

        function A() {

            alert("123");

        }

        var i;

        function StartInterval() {

            i = window.setInterval("A();", 3000);

        }

        function CloseInterval() {

            window.clearInterval(i);

        }

    </script>

</head>

<body>

    <input type="button" onclick="StartInterval();" value="打开" />

    <input type="button" onclick="CloseInterval();" value="关闭" />

</body>

</html>

(3)函数:setTimeOut(函数名,时间)——在间隔的时间里仅调用一次函数,注意函数要用双引号引起来。返回一个不确定的值,这个值可以传递给clearTimeOut ()来取消规划的函数

(4)函数:clearTimeOut(id)——关闭TimeOut();

例如:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script type="text/javascript">

        function A(obj) {

            alert(obj);

        }

        var i;

        function StartInterval() {

            i = window.setInterval("A('打开aInterval');", 3000);

        }

        function CloseInterval() {

            window.clearInterval(i);

        }

        var j;

        function StartTimeOut() {

            j = window.setTimeout("A('打开TimeOut');", 3000);

        }

 

        function CloseTimeOut() {

            window.clearTimeout(j);

        }

    </script>

</head>

<body>

    <input type="button" onclick="StartInterval();" value="打开aInterVal" />

    <input type="button" onclick="CloseInterval();" value="关闭InterVal" /><br />

    <input type="button" onclick="StartTimeOut();" value="打开TimeOut" />

    <input type="button" onclick="CloseTimeOut();" value="关闭TimeOut" />

</body>

</html>

9. document对象

将在下一部分讲解

10. document.cookie对象

(1)document.cookie的设置

例如:设置UserName=z3和Pwd=123

document.cookie = “UserName=z3; Pwd=123; ”;

 

设置过期时间用expires:

例如:设置UserName=z3和Pwd=123,过期时间为1天

var date = new Date();

date.setTime(date.getTime() + 1000 * 60 * 60 * 24 * 1);

document.cookie = “UserName=z3; Pwd=123; expires=”+date. toUTCString();

(2)document.cookie的获取(按上例来说)

        function GetCookie () {

            var kvDict = new Array();

            //UserName=z3; Pwd=123 //""

            if (document.cookie) {

                var arr = document.cookie.split("; ");

                for (var i = 0; i < arr.length; i++) {

                    var kv = arr[i].split("=");

                    kvDict[kv[0]] = kv[1];

                }

                document.getElementById("txtUserName").value = kvDict["UserName"];

                document.getElementById("txtPwd").value = kvDict["Pwd"];

            }

注意分隔字符串的时候是一个分号加上一个空格,一定要注意

posted on 2011-11-27 19:20  iYiming  阅读(227)  评论(0编辑  收藏  举报

导航