georg
georg

Reputation: 214949

document.ready on an ajax-loaded page

I'm using

$("#someDiv").load("ajax.html")

"ajax.html", in turn, contains a document.ready call:

 <script>$(function() { alert('works') })</script>

My question is when exactly this callback gets called. Is it safe to assume ajax.html to be completely loaded, parsed and scriptable at this point?

Upvotes: 2

Views: 180

Answers (1)

fmsf
fmsf

Reputation: 37137

Inside the bindReady

// Catch cases where $(document).ready() is called after the
// browser event has already occurred.

So I would say yes. Because when the html gets loaded. It calls the "createElement" which creates your html. On it's turn, during this process the bindReady is called. Which means that before your script get's called. All the html is already prepared and appended. As it is running in sequence.

CREATE HTML --> FINDS JS AND PROCESS --> binds ready --> Append --> (...) later on the ready function get's called because it is on the stack of callbacks

So I would answer yes to your question.

You should note that JS is not multithreaded. Even if some times it feels like it. So everything really does go into a sequence

Upvotes: 1

Related Questions