admin管理员组

文章数量:1336660

As the title suggested, I'm trying to call the $(document).ready(function() {...}); from another file. The code snippet is as below:

Source file:

$(document).ready(function () {
    alert('document.ready function called!');
    // a lot of code
}

And in the test file:

TestFile.prototype.testDocumentReadyContents = function () {
    // test code here trying to call the document.ready function
}

I haven't had any success on it yet. I have tried document.ready.apply(), trigger('ready'), overriding the document.ready function... but just couldn't call it. FYI I'm invoking it as part of my unit test.

Thanks.

As the title suggested, I'm trying to call the $(document).ready(function() {...}); from another file. The code snippet is as below:

Source file:

$(document).ready(function () {
    alert('document.ready function called!');
    // a lot of code
}

And in the test file:

TestFile.prototype.testDocumentReadyContents = function () {
    // test code here trying to call the document.ready function
}

I haven't had any success on it yet. I have tried document.ready.apply(), trigger('ready'), overriding the document.ready function... but just couldn't call it. FYI I'm invoking it as part of my unit test.

Thanks.

Share Improve this question asked Dec 9, 2009 at 0:41 BeraCimBeraCim 2,3478 gold badges50 silver badges78 bronze badges 2
  • What exactly do you mean by "another file"? Another JS file? An IFrame? An Ajax Request? – Pekka Commented Dec 9, 2009 at 0:52
  • @ Pekka: source file is a .js file. test file is also another .js file. Their includes are in a configuration file which works. – BeraCim Commented Dec 9, 2009 at 0:55
Add a ment  | 

1 Answer 1

Reset to default 9

GOOD WAY

$(document).ready(documentReady);

function documentReady() {
    alert('document.ready function called!');
    // a lot of code
}

TestFile.prototype.testDocumentReadyContents = function () {
    documentReady();
}

Hackish Way

TestFile.prototype.testDocumentReadyContents = function () {
    $.readyList[0]();
}

本文标签: javascriptCalling (document)ready(function() ) from another fileStack Overflow