views:

52

answers:

3

So when eval(data) is complete, how would you set a callback?

+1  A: 

Just put the code you want to execute on callback in the data that is going to be evaluated. Of course you will have problem if in the evaluated data you have some dynamic generated function that runs asynchronously.

Ilian Iliev
I'm trying to be outside of the `data`
Trip
Why? You can user data += 'my_function();'
Ilian Iliev
+3  A: 

Eval is not asynchronous, so you don't need a callback. Just put your function call on the next line.

The script you are evalling might do something asynchronous, in which case you would need to parse the JS, find the asynchronous code, and add a callback to that (in string form).

Best to avoid eval in the first place though. It is almost never the right solution to a problem.

David Dorward
Its a tough call about eval(data) as a good choice, because I'm calling an asynchronous request after the js has been loaded. But if I do clean my XSS, is there something more to worry about? And do you recommend any alternatives?
Trip
eval is difficult to debug, has scoping issues and is just plain slow. I can't suggest an alternative solution - I don't know the problem, only the function you think can solve it.
David Dorward
+1 for "Best to avoid eval in the first place." Eval can create a bunch of problems (including security risks, makes it very difficult to minify code, etc).
JasCav
+1  A: 

eval(data) is a blocking call. All you have to do is put your "callback" code after your eval() call, and it will be executed once the eval() is finished.

Ryan Kinal