JavaScript中BOM对象

Browser对象中包括window、navigator、screen、history、location对象。

(列举浏览器对象模型BOM里常用的对象:window、navigator、screen、history、location对象。)

浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器"对话"。

浏览器对象模型 (BOM)

浏览器对象模型(Browser Object Model (BOM))尚无正式标准。

由于现代浏览器已经(几乎)实现了 JavaScript 交互性方面的相同方法和属性,因此常被认为是 BOM 的方法和属性。

1.1 window对象

Window 对象表示浏览器中打开的窗口。

所有浏览器都支持 window 对象。它表示浏览器窗口。

所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。

全局变量是 window 对象的属性。

全局函数是 window 对象的方法。

甚至 HTML DOM 的 document 也是 window 对象的属性之一:

window.document.getElementById("header");

与此相同:

document.getElementById("header");

 

1.1.1 Window 尺寸

有三种方法能够确定浏览器窗口的尺寸。

对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:

  • window.innerHeight - 浏览器窗口的内部高度(包括滚动条)

  • window.innerWidth - 浏览器窗口的内部宽度(包括滚动条)

对于 Internet Explorer 8、7、6、5:

  • document.documentElement.clientHeight

  • document.documentElement.clientWidth

或者

  • document.body.clientHeight

  • document.body.clientWidth

注意:

document.body.clientHeight不能正确取到浏览器页面高度  ,原因是:

XHTML中用 document.documentElement.clientHeight 代替document.body.clientHeight

onresize 事件会在窗口或框架被调整大小时发生。

语法

<element οnresize="SomeJavaScriptCode">

JavaScript 中:

window.οnresize=function(){SomeJavaScriptCode};

<script>
//onresize 事件会在窗口或框架被调整大小时发生
window.onresize=function(){
// 三种写法,其中 前面两种写法 可以正确获取浏览器窗口的尺寸,第三种方法 不推荐使用
// var cw=document.documentElement.clientWidth;
// var ch=document.documentElement.clientHeight;
//document.title=cw+ '|' +ch;

var iw=window.innerWidth;
var ih=window.innerHeight;
document.title=iw+'|' +ih;

//var cW=document.body.clientWidth;
//var cH=document.body.clientHeight;
//document.body.clientHeight不能正确取到浏览器页面高度
//因为xhtml使用document.documentElement.clientHeight;代替了document.body.clientHeight这种写法
//document.title=cW+'|'+cH;

}

 预览:

1.1.2 其他 Window 方法

一些其他方法:

  • window.open() - 打开新窗口

  • window.close() - 关闭当前窗口

  • window.moveTo() - 移动当前窗口

  • window.resizeTo() - 调整当前窗口的尺寸

如果你在关闭窗口的时候,出现了如下问题:

js 关闭页面(Scripts may close only the windows that were opened by it.) 直接用window.close()无法关闭。

解决方法是:你关闭的窗口,要被其他窗口 先 打开。然后 你去执行关闭窗口的那个动作。

open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口。

语法:

window.open(URL,name,specs,replace)

 

url

name

specs

replace

详情参见:Window open() 方法 | 菜鸟教程

moveTo() 方法可把窗口的左上角移动到一个指定的坐标

window.moveTo(x,y)

如:

<button onclick="myWin()">打开窗口</button>
&emsp;&emsp;
<button onclick="moveWin()">移动窗口</button>

<script>
 // 注意:myWindow要作为全局变量 去使用。 因为这个变量要在多个函数体内使用。
        var myWindow = null;
        function myWin(){
            // 1.定义打开窗口的大小
            // '', 第一个参数,可以是空,代表 打开的是一个空白窗口
            // '', 第二个参数,可以是空,代表 打开的窗口可以没有名称
            // 'width=200,height=100' 一个逗号分隔的项目列表。中间不要写;分号,写分号,会出现bug,窗口大小显示不对!!!
            myWindow = window.open('','','width=200,height=100');
            // 2.向指定窗口中显示一些内容
            myWindow.document.write("这是我的窗口");
        }

        function moveWin(){
        //默认的最大化的浏览器的左上角
            myWindow.moveTo(0,0);
            myWindow.focus();
        }
    </script>

 预览:

resizeTo() - 调整当前窗口的尺寸

方法用于把窗口大小调整为指定的宽度和高度。

语法:

window.resizeTo(width,height)

 例如:

<button onclick="myWin()">创建窗口</button>
&emsp; &emsp;
<button onclick="resizeWin()">调整窗口</button>

<script>
function myWin(){
myWindow=window.open('','','width=100,height=100');
myWindow=document.write('这是我的窗口')
}
function resizeWin(){
myWindow.resizeTo(500,500);
myWindow.focus();
}
</script>

预览:

1.2.1 navigator对象

window.navigator 对象包含有关访问者浏览器的信息。

来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:

  • navigator 数据可被浏览器使用者更改

  • 一些浏览器对测试站点会识别错误

  • 浏览器无法报告晚于浏览器发布的新操作系统

  <div id="example"></div>
 <script>
    var txt=""
    txt+= "<p>浏览器代号: " + navigator.appCodeName + "</p>";
    txt+= "<p>浏览器名称: " + navigator.appName + "</p>";
    txt+= "<p>浏览器版本: " + navigator.appVersion + "</p>";
    txt+= "<p>启用Cookies: " + navigator.cookieEnabled + "</p>";
    txt+= "<p>硬件平台: " + navigator.platform + "</p>";
    txt+= "<p>用户代理: " + navigator.userAgent + "</p>";
    txt+= "<p>用户代理语言: " + navigator.language + "</p>";
    document.getElementById("example").innerHTML=txt;
    </script>

 预览:

注意:获取的某些信息 是不准确的。

1.2.2 screen对象

window.screen对象在编写时可以不使用 window 这个前缀。

一些属性:

  • screen.availWidth - 可用的屏幕宽度

  • screen.availHeight - 可用的屏幕高度

screen.availWidth 属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如窗口任务栏。

返回您的屏幕的可用宽度:

<script>
document.write("可用宽度: " + screen.availWidth);
</script>

 

screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。

返回您的屏幕的可用高度:

<script>
document.write("可用高度: " + screen.availHeight);
</script>

 

<script>
var aW=window.screen.availWidth
var aH=window.screen.availHeight
document.write('可用的屏幕宽度'+aW)
document.write('可用的屏幕高度'+aH)
</script>

 预览:(按f5,刷新页面)

1.2.3 history对象

window.history 对象包含浏览器的历史记录。

window.history对象在编写时可不使用 window 这个前缀。

为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。

一些方法:

  • history.back() - 与在浏览器点击后退按钮相同

  • history.forward() - 与在浏览器中点击向前按钮相同

history.back() 方法加载历史列表中的前一个 URL。

history forward() 方法加载历史列表中的下一个 URL。

<div id="main">
    <p>京东</p>
    <ul>
        <li><a href="#/login">登录</a></li>
        <li><a href="#/register">注册</a></li>
        <li><a href="#/user">用户中心</a></li>
    </ul>
    <div class="clear"></div>
    <div id="container">

    </div>
</div> 

<br><br><br>
<input type="button" value="后退" onclick="goBack()">
<input type="button" value="前进"onclick="goForward()">
<br><br><br>

 

<script>
    // 绑定事件 addEventListener()
    var main = document.querySelector('#main');
    var container = document.querySelector('#container');
    console.log(main,container)

    // hash变化时候,onhashchange触发下面的这个函数
    window.addEventListener('hashchange',function(){
        console.log(location.hash)
        switch(location.hash){
            case'#/login' :
                container.innerHTML = '<hr>我是登录页';
            break;
            case'#/register' :
                container.innerHTML = '<hr>我是注册页';
            break;
            case'#/user' :
                container.innerHTML = '<hr>我是用户中心页';
            break;
        }
    })

    //查看历史纪录对象
    console.log(history);
    //后退
    function goBack(){
        window.history.back()
 
    }

    //前进
    function goForward(){
        window.history.forward();
    }


</script>

预览:

1.2.4 location对象

window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。

window.location 对象在编写时可不使用 window 这个前缀。

一些实例:

  • location.hostname 返回 web 主机的域名

  • location.pathname 返回当前页面的路径和文件名

  • location.port 返回 web 主机的端口 (80 或 443)

  • location.protocol 返回所使用的 web 协议(http: 或 https:)

location.href 属性返回当前页面的 URL。

<a href="javascript:;" id="jump">跳转到百度</a>
    <script>
        var jump=document.querySelector('#jump')
        jump.onclick=function(){
            //location.href属性返回当前页面的URL(跳转到指定的网址,或者说打开指定的网址)
            window.location.href="https://www.baidu.com"
        }
    </script>

 

预览:

location.pathname 属性返回 URL 的路径名。

<script>
        console.log(window.location);
       
        // document.write(window.location.hostname)
        document.write(window.location.host+'<br>')
        document.write(window.location.pathname+'<br>')
        document.write(window.location.port+'<br>')
        document.write(window.location.protocol+'<br>')
        document.write(window.location.href)
       
    </script>

 预览:

location.assign() 方法加载新的文档。(相当于 打开窗口显示 百度,注意:打开的窗口会把原窗口给覆盖掉!)

 <img src="./img/1.png" alt="" id="pic">
    <script>
        var pic=document.querySelector('#pic')
        pic.onclick=function(){
            window.location.assign('https://www.youku.com')
        }
    </script>

 预览:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值