window.onload 与 $(document).ready() 区别

本文详细解释了JavaScript中window.onload事件与jQuery $document.ready函数之间的区别。window.onload在页面所有内容(包括DOM、异步JS、frames和图像)完全加载后触发;而$document.ready则在DOM加载完成后即刻执行,不等待其他资源加载。

A Windows load event fires when all the content on your page is fully loaded including the DOM (document object model) content and asynchronous JavaScript, frames and images. You can also use body οnlοad=. Both are the same; window.onload = function(){} and <body onload="func();"> are different ways of using the same event.

一个window load 事件触发条件是你页面完全加载时(页面完全加载包括DOM内容,异步JS,frames框架,和图像加载)。你可以用window.onload = function(){}<body onload="func();"> 添加事件监听器。

jQuery $document.ready function event executes a bit earlier than window.onload and is called once the DOM(Document object model) is loaded on your page. It will not wait for the images, frames to get fully load.

jQuery $document.ready 函数事件比 window.onload 执行找一点点。一旦页面DOM加载完成, $document.ready 就会被调用。它不会等待images,frame加载完成才调用。

From http://stackoverflow.com/questions/3698200/window-onload-vs-document-ready/18843427#18843427

测试代码如下所示,ready会先行于onload

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8"/>   
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
		<title>onload与ready测试</title>
		<style type="text/css">
			
		</style>
		<script type="text/javascript" src="jquery-2.1.4.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				alert('ready');
			});
			
			window.onload=function(){
				alert('load');
			}
			//ready 先行于 onload
		</script>
	</head>
	
	<body>
	</body>
</html> 

 

转载于:https://my.oschina.net/jallenkwong/blog/750128

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值