Home >Web Front-end >JS Tutorial >How to Evaluate Returned JavaScript Functions from Ajax Responses?
Integrating Ajax-Returned JavaScript Functions
The challenge arises when an Ajax response delivers a script block containing a JavaScript function. Execution of this function becomes necessary to trigger specific actions.
Solution: Evaluating the Function Code
Despite its placement within a
Consider the following simplified example:
<code class="html"><script> var newFunc = '<script>function go() { alert("Hello!") }</script>'; var e = document.getElementById('myElement'); e.innerHTML = newFunc; eval(document.getElementById('newFunc').innerHTML); </script></code>
In this scenario, Ajax is not used, but the concept remains the same. The script block is dynamically inserted into a
Additional Considerations
It's worth noting that this approach may not be optimal in all situations. Repeated Ajax invocations or concerns over function persistence within varying contexts may warrant alternative design solutions such as:
Ultimately, the best solution depends on the specific requirements and context of the application.
The above is the detailed content of How to Evaluate Returned JavaScript Functions from Ajax Responses?. For more information, please follow other related articles on the PHP Chinese website!