Dealing with globalEval and Safari -- suggestion for a better approach

Dealing with globalEval and Safari -- suggestion for a better approach


Hello,
I've been digging into jQuery's source to find out exactly how it
deals with the fact that Safari cannot eval code synchronously. My
conclusion is that I think jQuery's approach is flawed.
The globalEval() method does the actual evaluating of the code. Here
it is noted (in a comment) that Safari does not support synchronous
evals. As a result, using Safari it is not guaranteed that the code
evaluation is done as soon as the globalEval() method exits, while
with any other browser we do have this guarantee. Now, it would seem
logical to me to deal with this inconsistency inside globalEval(), so
that all other parts of the software do not have to worry about it.
This doesn't happen though. Inside load() an extra time out is set to
ensure that the loaded HTML (+ JS) is fully evaluated as soon as
load() exits, but no other parts of the jQuery library deal with this
inconsistency, leaving it up to the user of the library to make sure
all code is evaluated.
Just one example: when fetching some HTML + JS through an AJAX call
using ajax(), and then injecting that content into the DOM using
html(), the JS won't be evaluated yet as soon as html() exits.
My concern: why don't we deal with the asynchronous nature of an eval
in Safari in globalEval() itself? We can do this by adding a short
delay after the execution (evaluation) of the code has been started
and before globalEval() exits -- only in case we are dealing with
Safari of course.
I hope my point is clear. I'm curious for your opinions.
Tim Molendijk