onload事件和window,document,body的研究

今天在工作中用到了onload事件,发现了一些有趣的事情,比如一般来说,如果我们需要给一个DOM结构绑定一个事件,我们一般会采用如下方法(以Window对象为例):

【现象】

	window.onload = function() {
		console.log('<span style="font-family: Arial, Helvetica, sans-serif;">window.onload</span><span style="font-family: Arial, Helvetica, sans-serif;">');</span>
	};

        window.addEventListener && window.addEventListener("load", function() {
		console.log('window.addEvent');
	});

执行这两个方法,可以看到如下截图:

说明,这两个方法都被触发了,在这一点上IE表现也是一致的。

接下来我们来试试document对象

	document.onload = function() {
		console.log('document.onload');
	};

        window.addEventListener && document.addEventListener("load", function() {
		console.log('<span style="font-family:Arial, Helvetica, sans-serif;">document</span>.addEvent');
	});

	window.attachEvent && document.attachEvent('onload', function() {
		console.log('document.attachEvent');
	});

执行后发现,document的onload事件无论是哪种方式都不会触发。

再接下来,我们来试试body对象

	document.body.onload = function() {
		console.log('body.onload');
	};

	window.addEventListener && document.body.addEventListener("load", function() {
		console.log('body.addEvent');
	});

	window.attachEvent && document.body.attachEvent('onload', function() {
		console.log('body.attachEvent');
	});

结果如下图:

可以看到,说明通过这种写法只有onload方式绑定的被触发了。

还有更好玩的,当同时给body和window使用onload方法时,只有window的onload方法触发

具体代码如下:

	document.body.onload = function() {
		console.log('body.onload');
	};

	window.onload = function() {
		console.log('window.onload');
	}

执行结果如:

【结论】

所以总结如下:

1、onload和addEventListener当使用在window上的时候都触发

2、当使用在document时,都不触发

3、当使用在body上时,只有onload触发

4、当body和window同时使用onload方法时,body的方法会被吃掉。

【深入研究】

这里的研究主要是参看了w3c草案中的一些说明,怕翻译不好,贴出原文,貌似load事件和其他的有很多不同,

6.1.6.3 Event firing

Certain operations and methods are defined as firing events on elements. For example, the click() method
on the HTMLElement interface is defined as firing
click event on the
element. [DOMEVENTS]

Firing a simple event named e means that an event with the name e,
which does not bubble (except where otherwise stated) and is not cancelable (except where otherwise stated), and which uses the Event interface,
must be dispatched at the given target.

Firing a synthetic mouse event named e means that an event with the
name e, which does not bubble (except where otherwise stated) and is not cancelable (except where otherwise stated), and which uses the MouseEvent interface,
must be dispatched at the given target. The event object must have its screenXscreenYclientXclientY,
and button attributes set to 0, its ctrlKeyshiftKeyaltKey,
and metaKey attributes set according to the current state of the key input device, if any (false for any keys that are not available), its detailattribute
set to 1, and its relatedTarget attribute set to null. The getModifierState() method on the object must return values
appropriately describing the state of the key input device at the time the event is created.

Firing a click event means firing
a synthetic mouse event named click
, which bubbles and is cancelable.

The default action of these events is to do nothing except where otherwise stated.

6.1.6.4 Events and the Window object

When an event is dispatched at a DOM node in a Document in
browsing context, if the event is not a load event,
the user agent must also dispatch the event to the Window,
as follows:

  1. In the capture phase, the event must propagate to the Window object
    before propagating to any of the nodes, as if the Window object
    was the parent of the Document in the dispatch
    chain.
  2. In the bubble phase, the event must propagate up to the Window object
    at the end of the phase, unless bubbling has been prevented, again as if the Window object
    was the parent of the Document in the dispatch
    chain

另外就是,在第6.1.6.2节中有详细描述window,document,body对象对各事件的支持,因为太长,就不贴出来了。

Ps:本文纯属个人研究,如有谬误,烦请指出,万分感谢。

时间: 2024-08-14 04:50:38

onload事件和window,document,body的研究的相关文章

$(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法

$(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法接触JQuery一般最先学到的是何时启动事件.在曾经很长一段时间里,在页面载入后引发的事件都被加载 在”Body”的Onload事件里.对于Body的Onload事件和JQuery的Ready方法相比,有很多弊端.比如:1.加载 多个函数的问题<body ></body>在Onload事件中 只能这样加载,很丑陋…而在JQuery中你可以利用多个JQuery.Ready()

JavaScript基础 onload的必要性 window.document.body.bgColor代码写在了body标签的前面

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut

window onload 与 img onload事件

通常,window.onload就是加载完dom之后执行的.而img就是加载完图片完执行它的onload事件,根据img的src是否加载完成. 因此,看下面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http

window/body/img/iframe 的onload事件

在html页面中,只有body,img,iframe这一类标签具有onload事件. onload事件表示在当前元素载入完成后发生的事件.其中,window也有onload事件,但是跟body的是同一事件,如果页面上对两者都定义了该事件,只会响应一个. iframe上的事件在ie6下有一些bug: 1.iframe的onload事件跟window.onload事件只能二者取其一,似乎ie把它看作同一个事件了!例如: window.onload=function(){ alert("window

学习JQuery的$.Ready()与OnLoad事件比较

$(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法接触JQuery一般最先学到的是何时启动事件.在曾经很长一段时间里,在页面载入后引发的事件都被加载在"Body"的Onload事件里.对于Body的Onload事件和JQuery的Ready方法相比,有很多弊端.比如:1.加载多个函数的问题<body ></body>在Onload事件中只能这样加载,很丑陋-而在JQuery中你可以利用多个JQuery.

JavaScript - onload事件

onload事件 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test04.html</title> <script type="text/javascript"> var count=5; function show(){ for(var i=0;i<count;

jQuery的document ready与 onload事件——你真的思考过吗?

在进行实验和资料查询时,我遇到了几个关键问题: 1. window.onload到底是什么加载完触发? 2. body为什么会有onload事件? 3. 为什么是window.onload,而不是document.onload? 4. document ready到底是什么ready,DOM渲染完成? 5. jQuery怎么实现$(document).ready? 6. jQuery的ready,还能ready什么? 7. jQuery的document ready就一定比window.onlo

window.onload、DOMContentLoaded和$(document).ready()

window.onload.DOMContentLoaded和$(document).ready() <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <scr

jQuery $(document).ready()和JavaScript onload事件

对元素的操作和事件的绑定需要等待一个合适的时机,可以看下面的例子: <!DOCTYPE html> <meta charset="utf-8"> <html> <head> <title>1-1</title> <script type="text/javascript"> document.getElementById("panel").onclick = fu