JS_9_window对象

BOM浏览器对象模型:规范浏览器对js的支持,即js调用浏览器的功能。

它的具体实现为windows对象

 


一、框体方法

弹出一个框框,调用时候window可省略不写。

警告框:

  会显示一个确认按钮,返回undefined。

window.alert("警告语");

确认框:

  会显示一个确认按钮和取消按钮,点击确认时返回true,点击取消时放回false。

window.confirm("提示语");

提示框:

  会显示一个文本输入框和确认、取消按钮,若没有输入文本,则返回空字符串,若有则返回输入的内容。

window.prompt("提示语");

示例代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>学习</title>
        
        <script type="text/javascript">
            function testAlert(){
                window.alert("这是警告框提供警告信息。");
                alert("返回值:"+alert());
            }
            function testConfirm(){
                window.confirm("这是确认框,会提供确认按钮。");
            }
            function testPrompt(){
                window.prompt("这是提示框,会提供一个输入框。");
            }
            
        </script>
    </head>
    <body >
        <!--使用超链接调用js方法-->
        <a href="javascript:testAlert()" >超链接调用方法,测试警告框</a><br><br><br>
        <a href="javascript:testConfirm()" >超链接调用方法,测试确认框</a><br><br><br>
        <a href="javascript:testPrompt()" >超链接调用方法,测试提示框</a><br><br><br>
    </body>
</html>
View Code

二、定时执行

定时器:

  在指定的毫秒数之后执行指定的函数对象。

//开启一个定时器(线程),函数对象:函数名、"函数执行体"
var idTimeout= window.setTimeout(函数对象,毫秒数);
//关闭此计时器(线程)
window.clearTimeout(idTimeout);

间隔执行器:

  在指定的时间间隔后循环执行指定的函数对象。

//开启一个间隔执行器(线程)
var idInterval=window.setInterval(函数对象,毫秒数);
//关闭指定的间隔执行器(线程)
window.clearTimeout(idInterval);

示例代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>学习</title>
        
        <script type="text/javascript">
            var idTimeout;//定时器的标识
            var idInterval;//间隔器的标识
            
            function helloWorld(){ alert("hello world!");}
            
            function testSetTimeOut(){
                //4秒后执行指定的方法
                idTimeout=setTimeout(helloWorld,4000);
            }
            
            function testSetInterval(){
                //每隔2秒执行指定的方法
                idInterval=setInterval("alert('你好,两秒!')",2000);
            }
            
            function testClearTimeout(){
                //停止指定的定时器
                clearTimeout(idTimeout);
            }
            function testClearInterval(){
                //停止指定的间隔器
                clearTimeout(idInterval);
            }
        </script>
    </head>
    <body >
        <!--使用超链接调用js方法-->
        <a href="javascript:testSetTimeOut()" >超链接调用方法:测试延时执行</a><br><br><br>
        <a href="javascript:testSetInterval()" >超链接调用方法:测试间隔执行</a><br><br><br>
        
        <a href="javascript:testClearTimeout()" >点击此处停止计时器</a><br><br><br>
        <a href="javascript:testClearInterval()" >点击此处停止间隔器</a><br><br><br>
    </body>
</html>
View Code

三、子窗口

由于兼容问题,此方式不建议使用。

打开子窗口:

window.open('子页面url','打开方式','子页面配置');

关闭窗口:

window.close();

子窗口调用父页面的函数:

window.opener.函数名();

示例代码:

  父窗口:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>学习</title>
        <script type="text/javascript">
            //打开一个子页面
            function testOpen(){
                //参数:子页面的url,打开方式,子窗口页面配置
                //页面配置:大小位置、工具栏、滚动条、地址栏、菜单栏、是否可以改变窗口大小、状态栏
                window.open("son.html","_blank    ","height=100px,width=300px,top=300px,left=500px,\
                toolbar=no,\
                scrollbars=no,\
                location=no,\
                menubar=no,\
                resizable=no,\
                status=no");
            }
            
            //测试方法,由子页面调用
            function testSon(){
                window.alert("我被调用啦!");
            }
        </script>
    </head>
    <body >
        <input type="button" value="打开一个子窗口" onclick="testOpen()">
    </body>
</html>
View Code

  子窗口(son.html):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>子页面</title>
        <script type="text/javascript">
            function testClose(){
                window.close();
            }
            
            function testFather(){
                window.opener.testSon();
            }
        </script>
    </head>
    <body>
        <h3>这里是子页面</h3>
        <input type="button" value="关闭页面" onclick="testClose()">
        <input type="button" value="调用父页面的函数" onclick="testFather()">
    </body>
</html>
View Code

四、window常用属性

地址栏(location):

//通过改变地址栏地址,跳转到指定页面
window.location.href='指定页面的url';
//重新加载此页面
window.location.reload();

历史记录(history):

//页面前进一页
window.history.forward();
//页面后退一页
window.history.back();
//页面跳转到指定的历史记录,本页面为0
window.history.go(0);

屏幕属性(screen):

//浏览器屏幕的宽度
var w = window.screen.width;
//浏览器屏幕的高度
var h = window.screen.height;

浏览器配置属性:

window.navigator.userAgent

 

posted @ 2023-01-18 12:23  在博客做笔记的路人甲  阅读(19)  评论(0编辑  收藏  举报