browserify and document ready?

后端 未结 5 1020
慢半拍i
慢半拍i 2021-02-08 00:28

I\'m struggling with using Browserify and document ready events. How do I craft a module that exports content only available after the document ready event has fired? How do I

5条回答
  •  失恋的感觉
    2021-02-08 01:05

    This works for me in one project, not sure it can work always.

    Put the script, for example: right before .

    In the file going to be browserify the-file.js -o main.js.

    // the-file.js:
    
    var $ = require("jquery");
    
    //......
    
    window.$ = $;  // window is the global object in browsers
    $(document).ready(function(){
        // all the old things here ....
    });
    

    Another way might be better:

    // the-file.js:
    
    var $ = require("jquery");
    
    //......
    
    (function($){
    
        $(document).ready(function(){
            // all the old things here ....
        });
    
    })($); // Anonymous function get called with $.
    

提交回复
热议问题