简体   繁体   中英

Calling/Inserting functions before jQuery's document.ready

I need to be able to insert or call a function before document.ready() is called in jQuery. I'm invoking the document.ready function from another file, which theoretically gives me control over when document.ready is called.

Source file (.js):

$(document).ready(function () {
    // some code...
});

Technically, however, as soon as I call the file with the document.ready(), it is immediately loaded, and I had no chance of finding something similar to a pre-condition to insert and call my function.

Test file (also a .js):

// this is a unit testing file, JsTestDriver style
TestFunction.prototype.testFunction = function() {
    // calls my function
    // then calls the document.ready function below
    $.readyList[1]();
}

Since I'm calling the document.ready function from a .js unit test file, the "script" tag does not work for me. I need to be able to call my functions before the DOM (or document.ready) gets called. Does anyone know the answer to this one?

Thanks.

在document.ready之前放置您希望在document.ready之前执行的代码。

Override the ready function.

var fn = $.fn;
var realReady = fn.ready;
fn.ready = function ready()
{
    console.log("Before ready.");
    var result = realReady.apply(this, arguments);
    console.log("After ready.");
    return result;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM