ppk on javascript 笔记(六)--BOM

浏览器对象模型(Browser Object Model)是语言核心和DOM之间的一个过渡层,这个过渡层特指Javascript的客户端实现,它的主要任务是管理浏览器窗口并使得它们可以彼此通信。window对象是BOM的核心。事实上,window对象还定义了诸如当前页的URL、浏览器的识别字符串等非文档结构的部分。

WHAT-WG正在为BOM指定一个规范,BOM正遵循着Netscape 3的事实标准。

 

window对象

window对象首先是核心需要的全局对象,它包含了脚本中定义的所有全局变量和函数。

在一次会话中,一个window对象可以包含很多页面,新页面会被载入到同一个window对象,window对象原来的全局对象(变量和函数)将被销毁,但是window对象本身并没有被销毁,它仍旧可以被访问到,甚至用户关闭了主窗口。

有三种方法来保存上一次的全局变量,cookie,跨浏览器通信和保存到服务器,第三种方法是最安全的。

新窗口会创建一个新的window对象,当创建一个其他窗口的window对象的引用时,访问这个引用的属性将报错,原因在使用引用时,引用尚未完全载入。正确的方法应该是为两个窗口的存在和释放各记录一个cookie,然后再调用方法钱检查这些状态。

window和self都指向窗口本身,它们访问的是同一个对象,习惯上使用window。

可以为name属性赋值,window.name='myname',这将为链接的target属性提供一个引导。window.open()方法也可以用到这个name作为打开窗口的目标。

window.status属性直接访问并重写状态了文本,window.defaultStatus提供了没有状态时状态栏的文本。可以使用a标签的mouseover方法修改掉默认的鼠标滑过链接显示href的动作,特殊的,在onmouseover中,IE和Mozilla都需要你返回true,而不是false来阻止整个默认动作。

 

跨浏览器通信

必须同时满足两个条件才能进行窗口间的通信:两个窗口必须来自同一个域(同源策略);其中一个窗口包含另一个窗口的引用(即一个窗口打开了另一个窗口)。

window.open方法用来打开新窗口,完整地书写window.open()而不是open()是为了与document.open()区别。

window.open( ' page.html ' , ' popup ' , ' width=300,height=400 ' );

第一个参数指定载入的页面,为空则打开空白窗口,第二个参数设置新窗口的name值或打开已有name的窗口,第三个参数控制新窗口的外观和行为,详细的介绍在http://www.quirksmode.org/popups.html

保存window.open的返回值,则在主窗口创建了一个指向新窗口的引用,而新窗口总是自动创建一个opener属性指回主窗口。

当主窗口载入一个新页面时,弹出窗口的opner属性仍然指向主窗口(主窗口window对象没有销毁),但主窗口原来指向弹出窗口的全局变量被销毁了(此时仍满足跨窗口通信的第二个条件),我们需要重新建立联系:首先在弹出窗口建立一个函数,opener.newWindow=window(注意opener即为主窗口),按照上面的方法设置两个cookie来安全地在主窗口中调用弹出窗口的这个函数。考虑到同源策略和主窗口是否关闭,弹出窗口的这个函数应该写为:

if ( ! opener || opener.closed || ! checkCommunication()) {
// exit actions
} else if (checkCommunication() && opener.ST_loaded) {
opener.ST_newWindow
= window;
tryCounter
= 0 ;
}
else if (tryCounter < nrOfAttempts) {
// record the new site
tryCounter ++ ;
}
else {
// exit actions
}

首先判断opner为null(虽然罕见但是保险起见)、是否关闭、能否通信,然后检查主窗口的ST_loaded属性(自定义用来确认完全载入),最后判断尝试通信超过指定次数。

在这里我们用到了window.closed属性,我们讲窗口释放时所有的全局变量都被销毁,但是除少数浏览器外都保留了closed属性用来指示是否关闭,而对于其他属性诸如location和navigation是否仍存在,各个浏览器并没有对此达成一致,因此窗口释放时除closed属性外不要再使用。

(bloodmage按:在这里我提到了对象、变量、属性、方法、函数,实际上,javascript不区分属性和变量,也不区分方法和函数,只是习惯上单独使用时讲变量和函数,而谈到对象时则用属性和方法;我讲窗口释放时所有的全局对象都被销毁,是指window对象的属性和方法都被销毁了,而实际上window对象的属性即location,document.navigation,screen等也是对象,window对象的方法则主要有表示交互的alert和改变大小的resizeTo等。)

当违反同源策略(详见笔记一)时,不能从该窗口中收集它的任何信息,通过尝试来检查能否通信。

function checkCommunication() {
if ( ! opener) return false ;
try {
opner.testVar
= true ;
}
catch (e) {
return false ;
}
return true ;
}

window.close()方法用来关闭窗口,但一些浏览器拒绝执行,或者弹出一个对话框询问是否关闭窗口。通过javascript打开的窗口可以直接被关闭而不被提示,因此可以使用使用window.open('','_self');window.close();直接关闭当前窗口。

由于弹出窗口的恶名,浏览器默认开启Block unrequested popup windows功能阻止打开未经请求的弹出窗口,但这很容易被绕过,比如设置document.onclick = function() { window.open(); }。新的窗口拦截软件提供了更多的方法,但同时恶意广告也会利用flash、微软专有的popup对象或脚本创建内嵌层模拟弹窗。

 

导航

 

转载于:https://www.cnblogs.com/bloodmage/p/1789086.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
此书属网上搜集,供鉴赏之用,请勿保存,如触及到法律,本人概不负责,如喜欢 请购买正版。 目录: Introduction Chapter 1. Purpose Section A. Conceptual overview Section B. Technical overview Section C. JavaScript history Chapter 2. Context Section A. The CSS revolution Section B. Separation of presentation and structure Section C. Separation of behavior and structure Section D. Separation of behavior and presentation Section E. Accessibility overview Section F. Rules of accessibility Section G. The example scripts Chapter 3. Browsers Section A. The browsers Section B. Incompatibilities Section C. Object detection Section D. Browser detection Section E. Debugging Chapter 4. Preparation Section A. HTML and CSS structure Section B. Hooks Section C. Preparing the page Section D. The <script> tag Section E. Initialization Section F. The example scripts Chapter 5. Core Section A. The basics Section B. Data types Section C. Common operators Section D. Variables Section E. Working with numbers Section F. Working with strings Section G. Working with booleans Section H. Control structures Section I. Functions Section J. Objects Section K. Associative arrays Section L. Arrays Chapter 6. BOM Section A. The window object Section B. Cross-window communication Section C. Navigation Section D. Window geometry Section E. Miscellaneous Section F. The document object Section G. Cookies Chapter 7. Events Section A. Browser incompatibilities Section B. The events Section C. Event-handler registration Section D. Event bubbling and capturing Section E. The event object and its properties Section F. Targeting Section G. The example scripts Section H. Dropdown Menu, mouseouts, and the related target Chapter 8. DOM Section A. Concepts Section B. Finding elements Section C. Node information Section D. Changing the document tree Section E. Creating and cloning elements Section F. innerHTML Section G. Attributes Section H. Text nodes Section I. nodeLists Section J. Forms and the Level 0 DOM Section K. DOM hyperspace Section L. Markers Chapter 9. CSS modification Section A. The style property Section B. Changing classes and ids Section C. Writing CSS into the page Section D. Changing entire style sheets Section E. Comparison Section F. Showing and hiding elements Section G. Animations Section H. Dimensions and position of elements Chapter 10. Data retrieval Section A. Sending the request Section B. Handling the response Section C. The response format Section D. Accessibility Afterword The future of JavaScript
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值