$( document ).ready()&$(window).load()

时间:2023-03-09 19:32:51
$( document ).ready()&$(window).load()

$( document ).ready()

https://learn.jquery.com/using-jquery-core/document-ready/

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).load(function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$( document ).ready()</title>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
alert("document loaded");
});
    //document ready 简写
    $(function() {
    // ...代码...
    })
$(window).load(function () { alert("window loaded"); }); </script> </head> <body> <iframe src="http://techcrunch.com"></iframe> </body> </html>

Experienced developers sometimes use the shorthand $() for $( document ).ready(). If you are writing code that people who aren't experienced with jQuery may see, it's best to use the long form.

1
2
3
4
// Shorthand for $( document ).ready()
$(function() {
console.log( "ready!" );
});

You can also pass a named function to $( document ).ready() instead of passing an anonymous function.

1
2
3
4
5
6
7
8
9
// Passing a named function instead of an anonymous function.
function readyFn( jQuery ) {
// Code to run when the document is ready.
}
$( document ).ready( readyFn );
// or:
$( window ).load( readyFn );

DOMContentLoaded

https://developer.mozilla.org/zh-CN/docs/DOM/DOM_event_reference/DOMContentLoaded

当页面中的文档树解析完成时,在页面的Document对象上,会触发DOMContentLoaded事件.该事件代表了,页面的DOM树已经构建完成,但页面引用的样式表和图像文件,以及包含的框架页面可能还没有加载完成,在页面完全加载完成时,会触发另外一个类似的称为"load"的事件.

该事件会冒泡到当前页面的window对象上.但框架页面中文档加载完成时并不会触发父页面的DOMContentLoaded事件.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 0.2 1.0 (1.7 or earlier) 9.0 9.0 3.1
<script>
document.addEventListener("DOMContentLoaded", function(event) {
console.log("DOM fully loaded and parsed");
});
</script>

随机推荐

  1. [转]就这样,创建了自己的运行时共享库(RSL)

    原文地址:http://riaoo.com/?p=1405 博客园的下载地址(版权归原作者) http://files.cnblogs.com/tianlanliao/CustomRSL.zip 创建 ...

  2. JBPM4之decision节点:2、好学生|坏学生|超级学生

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...

  3. Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......

    mkdir jre cd jre ln -s 你的JDK目录/bin bin 例如:ln -s /usr/lib/jvm/jdk1.8.0_25/bin bin 原文地址:http://www.cnb ...

  4. base64 encoding

    //https://en.wikipedia.org/wiki/Base64 std::string base64Encode(const std::vector<char>& b ...

  5. CMDB反思5

    ITSM工具规划设计 http://blog.vsharing.com/xqscool/A946789.html 相比PPT中被管的数个对象(像培训什么的也都在其中),我们的需求其实就要小得多,但是问 ...

  6. C#拼音转换,将简体中文转换成拼音

    1. 要进行拼音转换操作,首先要引入几个文件,也就是用于操作拼音转换的文件,就是微软提供给开发者的一个类库 Microsoft Visual Studio International Pack 1.0 ...

  7. bzoj1013

    这道题题解太多,只贴代码. #include<cstdio> #include<cmath> #include<algorithm> using namespace ...

  8. webstorm启动bug

    场景描述: win10系统下,webstorm(32位)经常遇到无法启动的情况. 解决方案: 重启电脑. 1.win10系统需要更新时,webstorm无法启动,此为win10 bug,重启时,系统自 ...

  9. manacher算法_求最长回文子串长度

    很好的总结,转自: http://blog.****.net/dyx404514/article/details/42061017 总结为:两大情况,三小情况. 两大情况:I. i <= p 1 ...

  10. SlidingPaneLayout的基本使用

      SlidingPaneLayout是V4包中新添加的组件,可以实现两列面板的切换.说先来看看API文档的说明: ? 1 SlidingPaneLayout provides a horizonta ...