admin管理员组

文章数量:1327960

I am using jQuery. I keep all of my function definitions wrapped in the $(document).ready event in application.js. I have a function from it that I would like to call somewhere in the body of the page I am working on.

I was wondering if there is some alternative to the .ready event that will work on a script load. Ideally I would like to do something like: $('application.js').ready( call function );

In the jQuery documentation it only mentions the $(document).ready call but I was wondering if this can be altered or if there is some plain javascript alternative.

I am using jQuery. I keep all of my function definitions wrapped in the $(document).ready event in application.js. I have a function from it that I would like to call somewhere in the body of the page I am working on.

I was wondering if there is some alternative to the .ready event that will work on a script load. Ideally I would like to do something like: $('application.js').ready( call function );

In the jQuery documentation it only mentions the $(document).ready call but I was wondering if this can be altered or if there is some plain javascript alternative.

Share asked Mar 17, 2010 at 15:17 unfloresunflores 1,8102 gold badges16 silver badges37 bronze badges 2
  • 2 Why $(document).ready doesn't suite you? – Andrew Bezzub Commented Mar 17, 2010 at 15:21
  • Maybe I'm going about this the wrong way. In application.js the function was defined in the head wrapped by $(document).ready. I have an ajax call that only happens one page for the site so I wanted to call that function from within the page body. I wrapped the function call in $(document).ready as well. The function call is getting loaded before the function though. I would like a way to test the script load which will be after the page load. – unflores Commented Mar 17, 2010 at 15:52
Add a ment  | 

2 Answers 2

Reset to default 5

I think you’re looking for $.getScript. This jQuery function loads a JavaScript file from the server using a GET HTTP request and executes it. You can specify a callback function, to be executed after the script itself has been executed.

$.getScript('foo.js', function() {
 alert('foo.js was loaded, do something cool now');
});

I am curious about this myself, but instinct would tell me you'd want to wait for the document to finish loading before running any javascript.

本文标签: jqueryRun a javascript function on script loadStack Overflow