JS 基础 —— BOM
浏览器可视为一个对象,即window 对象,与浏览器相关的文档(document)、屏幕(screen)、历史记录(history)、当前访问位置(location)、浏览器信息(navigator)都是其属性,而定时器、模式弹窗等都是其方法。window的子对象可不写window.前缀。
一、window对象
属性:
window.innerHeight:当前html页面的高度(注意不是整个浏览器窗口!整个浏览器窗口用screen对象);
window.innerWidth:当前页面的高度。
对IE 8~5,对应属性应为:
document.documentElement.clientHeight/clientWidth 或 document.body.clientHeight/clientWidth。
通用写法:var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
方法:
window.open(“url”):打开一个新标签页,显示url对应的页面;
window.close():关闭当前标签页(注意是标签页,所以如果一个标签页被划分显示多个html,则在其中一个html中使用此方法将无效);
window.moveTo(x,y):移动窗口;
window.resizeTo(w,h):调整窗口大小。
window.setTimeout(字符串脚本或function,delayTime):设置延时执行,如:window.setTimeout(“alert(‘2秒’)”,2000);
window.setInterval(字符串脚本或function,spanTime):定期不断执行;
window.clearTimeout(句柄)、window.clearInterval(句柄):关闭定时事件。
简单秒表:
var seconds = 0;
var timmer = window.setInterval(“document.getElementById(‘displaySeconds’).innerHTML= ++seconds”,1000);
二、window.location对象
属性:
以一个Test工程的index.html测试页面完整路径为例:http://localhost:8080/Test/index.html
location.hostname:返回web主机域名,即localhost;
location.pathname:返回当前页面的路径和文件名,即相对路径:/Test/index.html:
location.port:web主机端口,即8080;
location.protocol:返回使用的web协议(http://或https://),即http://;
location.href:返回当前页面的URL,即完整路径:http://localhost:8080/Test/index.html。
即 href = protocol+hostname+”:”+port+pathname
方法:
location.assign(“http://www.baidu.com"):加载新的文档;
location.reload():重新加载当前文档,相当于刷新;
location.replace(“www.baidu.com”):和assign方法差不多。
三、window.history对象
属性:
history.length:返回历史记录数量。
方法:
history.back():后退到上个页面;
history.forward():与浏览器向前按钮相同;
history.go(number|URL):加载记录表中某个具体页面。参数为数字时以当前页面为0,向后叠减去1,向前叠加1
如:history.go(-1) 相当于location.back(),最多为history.go(1-history.length)。
四、window.screen对象
属性:
screen.availWidth:可用的屏幕宽度(指整个浏览器能展示页面的部分的宽度,而非某个单独的html页面——window.innerWidth);
screen.avalHeight:可用的屏幕高度(同上,都指眼睛直接可见的部分,不包括未滚动出的部分);
screen.width:显示器宽度(如通常的1366*768中的1366);
screen.height:显示器高度。
screen.updateInterval:设置或返回屏幕刷新率(整数)。
五、window.navigator对象
属性:
navigator.appCodeName:代码名,以Netscape代码为基础的浏览器中,名都为Mozilla;
navigator.appName:浏览器内核名;
navigator.appVersion:版本;
navigator.cookieEnabled:是否支持cookie;
navigator.platform:平台,如win32;
navigator.userAgent:用户代理;
navigator.systemLanguage:系统语言。
注:navigator对象的信息具有误导性(因为它可被浏览器使用者更改;同时浏览器无法报告晚于其发布的新操作系统——这个就算了吧),不应该被用于识别浏览器,最好根据浏览器差异性识别。
六、window.document.cookie对象
还没有评论,来说两句吧...