最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling $(document).ready(function() {...}); from another file - Stack Overflow

programmeradmin0浏览0评论

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]();
}
发布评论

评论列表(0)

  1. 暂无评论