当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript BOM学习

JavaScript BOM学习

2019年11月14日  | 移动技术网IT编程  | 我要评论

mirror王宇阳

2019年11月13日 [首发]

数日没有更新博文了,觉得不好意思了!这不是,整理了一下javascript的一下bom笔记资料,今天贡献出来!(html dom也会随后整理发表)

笔者在接触js之前就听闻js的“牛逼”,接触后发现只要想法够贼,js就能给你的贼想法复现 ~

作者主页:

bom简单的说就是浏览器对象模型,对bom的操作就是对浏览器的功能和属性的操作;

bom的核心是window,它是一个浏览器的功能实例,浏览器会为html文档创建一个专属的window对象,并为每一个框架创建额外的window对象。
window对象是bom的顶层,所有其他对象都是通过window对象衍生的;但是在调用子对象的时候并不强制要求声明

dom的document也是window的子对象之一;以下两种写法是相同的:

    window.document.getelementbyid("herd")
    document.getelementbyid("herd")

window对象常用方法

弹窗

window.alert() 消息框;弹窗会直接显示一段信息字段

window.confirm() 确认框;弹窗显示text字段的同时给出确认和取消两个按钮,返回true和false

window.prompt() 提示框;弹窗显示字段和一个输入框,并返回输入框内容

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <input type="button" value="警告框" onclick="showalert()" />
        <input type="button" value="确认框" onclick="showconfirm()" />
        <input type="button" value="提示框" onclick="showprompt()" />
        <script>
            function showalert(){
                window.alert("这是一个警告框");
            }
            function showconfirm(){
                window.confirm("这是一个确认框");
            }
            function showprompt(){
                window.prompt("这是一个提示框");
            }
        </script>
    </body>
</html>

浏览器窗口信息

window.open() 打开新窗口

window.open( url , name , features , replace )

url:需要载入的网页url地址

name:为新窗口命名

features:可选,窗体的特性定义

属性 特性
height 窗口高度
width 窗口宽度
left 左边距
top 左上边距
directories 是否显示链接工具栏
location 是否显示地址栏
menubar 是否显示菜单栏
resizable 是否允许调整窗口尺寸
scrollbars 是否显示滚动条
status 是否显示状态栏
toolbar 是否显示工具栏

window.close() 关闭当前实例化对象的窗口

window.moveto() 移动当前窗口

window.resizeby()/resizeto() 调整窗口

window.focus() 获得当前对象窗口的焦点

window.blur() 释放当前对象窗口的焦点

window.print() 打印当前窗口或frame

window.scrollby()/scrollto() 滚动当前窗口总的html文档

setinterval()/clearinterval() 设置定时器

settimeout()/cleartimeout() 删除定时器

浏览器窗口尺寸

  • ie、chrome、firefox、opera、safan

    window.innerheight 浏览器窗口的内部高度

    window.innerwidth 浏览器窗口的内部宽度

  • ie8以下版本

    document.documentelement.clientheight 高度

    document.documentelement.clientwidth 宽度

    document.body.clientheight 高度

    document.body.clientwidth 宽度

通常在javascript中,一段较为通用代码如下:

var w = window.innerwidth || document.documentelement.clientwidth || document.body.clientwidth ;
var h = window.innerheight || document.documentelement.clientheight || document.body.clientheight ; 

窗口页面绝对居中

// 页面绝对居中必须设置style属性: position:absolute;
var left = (w-width)/2;
var top = (h-height)/2;
// 利用margin外边距的top和left来“绝对居中”在浏览器中间
document.getelementbyid("cen").style.top =  top+"px";
document.getelementbyid("cen").style.left = left+"px";

浏览器信息:navigator

navigator.appcodename 返回浏览器代码名称(网景 mozilla)

navigator.appname 返回浏览器名称

navigator.appversion 返回浏览器版本号

navigator.platform 返回浏览器操作系统

useragent 返回包含内码名称、版本号;用于识别用户

<input type="button" value="浏览器信息" onclick="showversion()"/>
function showversion(){
    // navigator 浏览器信息
    var put = document.getelementbyid("version");
    put.innerhtml = "代号:"+navigator.appcodename+"<br/>";
    put.innerhtml+= "名称:"+navigator.appname+"<br/>";
    put.innerhtml+= "版本:"+navigator.appversion+"<br/>";
    put.innerhtml+= "系统:"+navigator.platform+"<br/>";
    put.innerhtml+= "信息:"+navigator.useragent+"<br/>";              
}

屏幕对象:screen

属性对象 特性
screen.height 回显屏幕高度
screen.width 回显屏幕宽度
screen.avaiheight 回显除任务栏的屏幕高度(可用的高度)
screen.avaiwidth 回显除系统部件宽度的宽度(可用的深度)
screen.colordepth 浏览器分配的颜色或颜色深度
screen.pixeldepth 返回屏幕的颜色分辨率(色彩分辨率)
<input type="button" value="屏幕信息" onclick="showscreen()" />
function showscreen() {
    document.getelementbyid("screen").innerhtml = "屏幕尺寸:"+screen.height+"*"+screen.width+"<br/>";
    document.getelementbyid("screen").innerhtml+= "窗口尺寸:"+screen.availheight+"*"+screen.availwidth+"<br/>";
    document.getelementbyid("screen").innerhtml+= "色彩深度:"+screen.colordepth+"/色彩分辨率:"+screen.pixeldepth+"<br/>";
}

地址对象:location

地址对象整理的是当前的url信息

属性 特性
location.href 整个url字符串
location.protocol url的通信协议部分的字符子串
location.hostname url中服务器名、域名子域名或ip地址
location.port url中端口号
location.host hostname + port
location.pathname url中的文件或路径名
location.hash url中的锚点名称
location.search url中表示变量的字符子串
location.reload(true/false) 刷新页面(true/false选择是否从服务器刷新)
location.replace(url) 通过url网址刷新当前网页

历史对象:history

历史对象保存着用户上网的历史记录

属性方法 特性
history.back() 显示浏览器的历史列表中后退一个网址的网页
history.forward() 显示浏览器的历史列表中前进一个网址的网页
history.go(n)/go(url) 显示浏览器的历史列表中的第n个网址网页,大于0表示前进,小于0表示后退,等于0表示刷新当前页

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网