js进阶五:BOM

2020-01-17  本文已影响0人  蘭小木

BOM 浏览器对象模型

我们的BOM对象都是作为window对象的属性保存的,
我们可以通过window对象来访问

window

代表浏览器的窗口

setInterval()

定时调用

clearInterval()

关闭一个定时调用
需要一个定时器的标识作为参数,需要用它来指定要关闭的定时器

clearInterval(timer);

setTimeout()

延时调用

clearTimeout(timer);

关闭延时调用


window.navigator.geolocation

定位

getCurrentPosition( function1(position){}, function2(err){}, optionObj);

watchPosition(function1(position){}, function2(err){}, optionObj);

等待系统发出位置改变信号(不会主动轮询),该函数返回一个标识符。用于跟踪监控操作。clearWatch()通过该标识符取消监控操作(类似setTimeout()和clearTimeout())。
该函数参数及作用与getCurrentPosition()完全相同。

clearWatch()

关闭获取定位

navigator

代表的浏览器的信息,通过该对象可以来识别不同的浏览器

    //获取浏览器信息
    var ua = navigator.userAgent;
        console.log(ua);
        //通过UserAgent来判断浏览器的信息
        if(/firefox/i.test(ua)){
            alert("你是火狐~~~");
        }else if(/chrome/i.test(ua)){
            alert("你是chrome浏览器~~~");
        }else if(/msie/i.test(ua)){
            alert("你是IE");
        }else if("ActiveXObject" in window){
            alert("你是IE11");
        }

history

代表的浏览器的历史记录,通过该对象可以控制浏览器向前或向后翻页

history.back();

退回到上一个页面

history.forward();

进入下一个页面

history.go(-2)

跳转到指定页面,需要一个整数作为参数

history.length

历史记录数量

history.pushState("lalal", null, "learn.html");

向浏览器中添加一条历史记录

history.replaceState("lalalal", null, "learn.html");

替换当前历史记录

window.onpopstate事件

需要按浏览器上的后退或前进按键才会触发

window.onpopstate = function (ev) {
console.log(ev.state);
}

location

代表浏览器的地址栏的信息,通过该对象可以控制浏览器跳转页面

location = "test01.html";

跳转到指定页面,会生成历史记录可以使用会退按钮回退

location.assign("test02.html");

和直接修改location是一样的

location.replace("test02.html");

跳转到其他页面,不会生成历史记录。

location.reload(true);

可以用来刷新当前网页,相当于浏览器的刷新按钮

screen

代表用户的屏幕信息

上一篇 下一篇

猜你喜欢

热点阅读