3

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.

2
  • Can you load a page with the stuff and then redirect to a different page? Not great technique, but what you're doing sounds very kludgy. ready is already a hack to get things to execute before onload Commented Jan 5, 2010 at 3:52
  • If you want to call stuff before the DOMReady event, just don't put it in the event wrapper, or include your pre-condition in a script that is included before the rest of your scripts. Commented Jan 5, 2010 at 4:44

2 Answers 2

2

Place the code you wish to execute before document.ready, before document.ready.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried that before with no success. In the test file, as soon as TestFunction.prototype.testFunction starts, document.ready had already been loaded (I saw it with an alert). In the source file, it is not possible since I'm only calling the document.ready function, not the code before or after it.
0

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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.