3

I need to include a script via $.getScript dynamically.

The problem is however, that the $(document).ready function from that dynamically included script is not executed. (They dynamic insertion does not happen at dom ready)

Is there some way to force it to do so? By, for example, faking a document.ready event?

Thanks, Wesley

  • possible duplicate of [jquery : trigger $document.ready (so AJAX code I can't modify is executed)](http://stackoverflow.com/questions/2238030/jquery-trigger-document-ready-so-ajax-code-i-cant-modify-is-executed) – James Wiseman Jun 28 '11 at 07:37

3 Answers3

3

After doc ready has been fired any future doc ready calls will be executed when parsed.

Read the API

The handler passed to .ready() is guaranteed to be executed after the DOM is ready

redsquare
  • 78,161
  • 20
  • 151
  • 159
  • Thank you, it appears the fault was in another section of the code. This works just as you described. –  Jun 28 '11 at 14:38
0

It makes sense that new handlers defined through $.getScript() are missing the document.ready event since the document's DOM finished loading way earlier.

Have you tried $(document).trigger('ready') in order to re-trigger that event? Of course, all event handlers registered earlier would be called again.

cg.
  • 3,648
  • 2
  • 26
  • 30
  • `$(document).ready` handlers execute immediately if the document is already loaded. (The same is not true for `$(document).bind('ready'`, though.) – Tgr Jun 28 '11 at 07:29
0

In your include file, move the initialisation out to another function and call it from there:

//include js:
$(document).ready(initialise);

function initialise(){
}

This in the file that loads this all you need do is call initialise

The subject of triggering the ready even is already well covered on SO:

How to trigger $().ready() in jQuery?

Trigger $document.ready (so AJAX code I can't modify is executed)

Community
  • 1
  • 1
James Wiseman
  • 29,946
  • 17
  • 95
  • 158