js bom 四大对象

ECMAScript是JavaScript的核心,但在web使用JavaScript,那么BOM(浏览器对象模型)才是真正的核心。
BOM的核心对象是window,它表示浏览器的一个实例。
在浏览器中,window对象既是JavaScript访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象。也就是说,在网页中定义的任何一个变量、对象和函数以window作为其Global对象。

具体操作可到下面查询

https://developer.mozilla.org/en-US/

<h3>1.window对象</h3>
<h6>window常用方法</h6>

open(): 在一个窗口中打开页面
setInterval(): 设置定时器(执行n次)
setTimeout(): 设置定时器(只执行1次)
clearInterval(): 清除定时器
clearTimeout(): 清除定时器
alert(): 提示框
confirm(): 确认提示框
propmt(): 输入提示框</br>
注意:因为window对象使用非常频繁,所以当调用js中的window对象的方法时,可以省略对象名不写。

<style type="text/css">
    input{
        margin-top:10px;        
    }
</style>

<script type="text/javascript">
    function testAlert(){
        window.alert("测试alert");    
    }

    /*
    参数1:dialog中显示的内容
    参数2,3:可点击的按钮(默认没有就是"ok","cancle")
    返回值: ok->true cancle->false
    */
    function testConfirm(){
        var flag;
        flag = window.confirm("你这是在作死你知道吗?","知道","不知道");
        if (flag)
            window.alert("知道你还作死?");
        else
            window.alert("你注定死无全尸!");
    }
    
    /*
    参数1:可以是一个资源地址(图片,本地地址...)
    参数2:target of links
    参数3:窗口特点......
    */
    function testOpen (){
        window.open("http://www.baidu.com","_blank","height=400px,width=400px,left=10px");
    }
    
    /*
    参数1:定时器要执行的方法
    参数2:定时器时间
    */
    var intervalID;
    function testInterval (){
        intervalID = window.setInterval("testOpen()",2000);//2秒弹一个广告
    }
    
    /*
    参数1:定时器要执行的方法
    参数2:定时器时
    */
    var timeoutID;
    function testTimeout(){
        timeoutID = window.setTimeout("testOpen()",1000);   
    }
    
    /*
    参数1:提示语
    返回值:在输入框中输入的内容
    */
    function testPrompt(){
        var str;
        str = window.prompt("请输入您上的银行卡密码:");    
        window.alert("您刚才输入了:"+str);
    }
    
    /*清除两个定时器*/
    function clearInterval(){ 
        window.clearInterval(intervalID);   
    }
    
    function clearTimeout(){
        window.clearTimeout(timeoutID); 
    }
</script>
</head>

<body>
    <input type="button" value="testAlert" onclick="testAlert()" /></br>
    <input type="button" value="testOpen" onclick="testOpen()" /></br>
    <input type="button" value="testConfirm" onclick="testConfirm()" /></br>
    <input type="button" value="testInterval" onclick="testInterval()" /></br>
    <input type="button" value="testTimeout" onclick="testTimeout()" /></br>
    <input type="button" value="clearInterval" onclick="clearInterval()" /></br>
    <input type="button" value="clearTimeout" onclick="clearTimeout()" /></br>
</body>

<h3>2.location对象</h3>

常用方法

***href属性: ***代表的是地址栏的URL,可以获取和设置URL。URL表示统一资源定位符
reload方法: 刷新当前页面

<script type="text/jscript">
    function testHref(){
        //alert(window.location.href);  显示当前网址
        
        //通过修改location对象的href属性来实现页面的跳转
        window.location.href="http://www.baidu.com";
    }
    
    function testReload(){
        //刷新当前页面
        window.location.reload();   
    }
</script>
</head>

<body>
    <input type="button" value="跳转" onclick="testHref()">
    <input type="button" value="刷新" onclick="testReload()">
</body>

3.history对象

常用方法:

forward(); 向前
back();向后
go(n);n为正时向前n页,n为负时后退n页

<script type="text/javascript">
    function testForward(){
        window.history.forward();
    }
    
    function testBack(){
        window.history.back();
    }
    
    function testGo(){
        window.history.go(1);
    }
    
    function testGo2(){
        window.history.go(-1);
    }
</script>

</head>
<body>
    <input type="button" value="往前" onclick="testForward()"/>
    <input type="button" value="退后" onclick="testBack()"/>
    <input type="button" value="往前n" onclick="testGo()"/>
    <input type="button" value="退后n" onclick="testGo2()"/>
</body>

4.screen对象

<script type="text/javascript">
    /*
        availHeight和availWidth是排除了任务栏之后的当前屏幕(物理上的)高度和宽度
    */
    document.write(window.screen.availWidth + "<br/>");
    document.write(window.screen.availHeight + "<br/>");
    document.write(window.screen.width + "<br/>");
    document.write(window.screen.height + "<br/>");

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

推荐阅读更多精彩内容