JS之BOM

47 阅读1分钟

JS之BOM

1.BOM

bom其实就是一些浏览器对象像window location screen history

  • window

alert()

    alert('hetao');
    window.alert('hetao');//和上面一样
    

confirm

var ret =  window.confirm('你确定吗'); //这个弹窗有 确定取消两个按钮 分别返回不同的值 true faluse
    console.log(ret)

prompt

    var neir =  window.prompt('请输入内容','弹筐里的默认值'); //弹窗输入框 返回你输入的值 第二个值为弹窗里的默认值
    console.log(neir)

定时器方法 setTimeout() setInterval()

    window.setTimeout(function(){
    console.log('5秒后我才出来');
    },5000)//两个参数 第一个参数为函数 第二个参数为时间 5000ms
    console.log('第二个函数') //不管setTimeout 设定掩饰时间是多少都会先执行 第二个函数!
    
    //-----------------------------------
    
    setInterval(function(){
        console.log('循环执行我')
    },1000);//循环执行函数 第二个为循环执行时间
    //我们可以使用clearInterval() 来取消循环执行
    
    var time = 0;
    var fo = setInterval(function(){
            time++;
            console.log(time);
            if(time > 9){
                clearInterval(fo);
                console.log('取消循环')
            }
    },1000)
  • location

主要是获取地址栏的信息有以下几种方法

    console.log(location.host);
    // location.hostname 
    // location.href  跳转
    // location.replace 跳转 不产生历史记录
    // location.pathame 
    // location.port 端口号
    // location.protocol 协议
    // location.search 
    // location.reload 重载
  • navigator

检测当前浏览器的插件

    console.log(navigator);
    // plugins
    function hasPlugin(){
        //如果有插件 返回 true 相反 faluse
        name = name.toLowerCase();
        for(var i=0;i<navigator.plugins.length;i++){
       if( navigator.plugins[i].name.toLowerCase().indexOf(name)>-1){
       //有此插件
           return true
       }else {
           return false
       }
        }
    }
    alert(hasPlugin('Flash'))
  • history
    history.go(1);  // 前进一个页面
    history.go(-1); // 后退一个页面