GithubHelp home page GithubHelp logo

requirejs-domready's Introduction

requirejs-domReady

测试并验证requirejs domReady事件的触发时间点


html

<html>

<head>
    <title>requirejs domReady test</title>
</head>

<body>
    <img onload="console.log('image 1 loaded');" src="image1">
    <img onload="console.log('image 2 loaded');" src="image2">

    <script src="http://cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/require.js/2.1.18/require.min.js"></script>
    <script>
        require(['main.js'], function() {});
    </script>
</body>

</html>

main.js

define([
    'domReady'
], function (domReady) {
    $(document).ready(function () {
        console.log(document);
        var childNodes = document.body.childNodes;
        console.log('childNodes number: ' + childNodes.length);
        for (var i = 0; i < childNodes.length; i++) {
            console.log(childNodes[i] + ': ' + childNodes[i].nodeType);
        }
        console.log('jquery domReady');
    });
    domReady(function () {
        console.log('requirejs domReady');
    });
});

运行结果

qq 20160324152236

**结论:**可以看到,jquery的ready方法在DOM树构建后即触发,而requirejs的domReady方法则是在所有资源被加载完毕之后触发,相当于window.onload。

requirejs-domready's People

Contributors

jsoon avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.