I think it’s correct to interpret your question in this form: “Well, I’ve already finished with all Ajax materials, I just want to know if the JavaScript function can my Ajax callback inserted into the DIV at any time this moment, that is, I don’t want to call it contextual callback. "
OK, if you mean something like this, yes, you can call your new code at this time at any time while saving the page in the browser under the following conditions:
1) The JavaScript code returned by the Ajax callback should be syntactically OK,
2) Even if a function declaration is inserted into a <script> block in an existing <div> element, the browser will not know that a new function exists, since the declaration code has never been executed. So you must eval() your declaration code returned by the Ajax callback to efficiently declare your new function and have it available throughout the life of the page.
Even if pretty dummy, this code explains the idea:
<html> <body> <div id="div1"> </div> <div id="div2"> <input type="button" value="Go!" onclick="go()" /> </div> <script type="text/javascript"> var newsc = '<script id="sc1" type="text/javascript">function go() { alert("GO!") }<\/script>'; var e = document.getElementById('div1'); e.innerHTML = newsc; eval(document.getElementById('sc1').innerHTML); </script> </body> </html>
I did not use Ajax, but the concept is the same (even if the example I chose is not very smart :-)
Generally speaking, I do not question your solution design, i.e. it is more or less appropriate to screen + generalize the function in a separate .js file, etc., but please note that such a solution can cause additional problems, especially if your Ajax calls should be repeated, i.e. if the context of the same function should change, or if the problem with the declared function should be affected, you may need to seriously consider changing the design to one of the suggested examples in this thread.
Finally, if I misunderstood your question, and you are talking about a function context call when the Ajax callback is returned, then I feel like I am proposing the Prototype approach described by krosenvold , as it is cross-browser, tested and fully functional, and this can give you the best roadmap for future implementations.