$(document).ready vs $(window).load vs window.onload
原文地址: $(document).ready vs $(window).load vs window.onload
We execute our code when DOM is ready except images.
//call type 1
$(document).ready(function() {
/** work when all HTML loaded except images and DOM is ready **/
// your code
}); //call type 2
$(function() {
/** work when all HTML loaded except images and DOM is ready **/
//your code
}); //call type 3
$(document).on('ready', function(){
/** work when all HTML loaded except images and DOM is ready **/
//your code
}); //call type 4
jQuery(document).ready(function(){
/** work when all HTML loaded except images and DOM is ready **/
//your code
});
It is work when all DOM is ready including images so it is useful when on document load we want to work with images.
$(window).load(function() {
/** this is come when complete page is fully loaded, including all frames, objects and images **/
});
The onload event is a standard event in the DOM, while above two are specific to jQuery . this is also same functionality like $(window).load but window.onload is the built-in JavaScript event.The onload event occurs when an object has been loaded.like if we take a example of image and call onload event in image tag then it will call when image will load .generally we use it in body tag.
In HTML
<element onload="myFunction"></element>
In JS
object.onload=function(){/**your desire code**/};// here object can be window,body and etc
1) Here alert “call on body load” call immediately after body has been loaded
// In HTML
<!-- on body onload call myFunction -->
<body onload="myFunction()"> //In JavaScript
// myFunction() which will call on body load
function myFunction(){
alert("call on body load");
}
2) Here alert “call on image load” call immediately after image has been loaded
// In HTML
<!-- on image onload call myImageFunction() -->
<img src="data:image path src" onload="myImageFunction()"> // // myFunction() which will call on image load
function myImageFunction(){
alert("call on image load");
}
window.onload Examples
The function fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
window.onload = function() {
init();
doSomethingElse();
};
以上です。
随机推荐
- [BS-09] UITabBarController简单介绍
iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
- Java final,static 关键字
final关键字: 这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量. final类不能被继承,没有子类,final类中的方法默认是final的.final方法不能被子类 ...
- MongoDB的C#封装类
代码: samus驱动 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- 通常Struts框架会自动地从action mapping中创建action对象
开发者不必在Spring中去注册action,尽管可以这么去做,通常Struts框架会自动地从action mapping中创建action对象 struts2-spring-plugin-x-x-x ...
- mysqld_multi部署mysql单机多实例
1.安装gcc-c++.ncurses依赖包 # yum install gcc-c++ ncurses-devel 2.安装cmake,用来编译mysql # tar -xvf cmake-3.2. ...
- vmware lan map
1,nat: virtrual machine---vmnet8---PC---internet 2,host-only virtual machine(0,1,2...)---vmnet1 3,br ...
- Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)
控制台程序 定义Point类: public class Point { // Create a point from its coordinates public Point(double xVal ...
- 《30天自制操作系统》05_day_学习笔记
//bootpack.c 完整代码 #include <stdio.h> void io_hlt(void); void io_cli(void); void io_out8(int po ...
- C#面向对象的方法写数组的功能
上一篇文章用Java方法写出了可以对数组执行的功能,然后在用实例化后的对象调用这些方法来实现这些功能: 这篇随笔改用C#语言实现同样的功能 方法类:Array using System; using ...
- java ajax传值 中文乱码
String remark = new String(this.getRequest().getParameter("remark").getBytes("iso885 ...