\\';var e = document.getElementById(\\'div1\\');e.innerHTML = newsc;eval(document.getElementById(\\'sc1\\').innerHTML);
This example inserts the JavaScript function declaration into a DIV element and evaluates it, making the go() function available for execution.
Execution Timing
It's important to note that the function is available for invocation only after the evaluation process is complete. The exact timing may vary depending on the Ajax implementation and response handling.
","image":"http://www.luping.net/uploads/20241031/173037528667236e769a7b8.jpg","datePublished":"2024-11-04T04:26:28+08:00","dateModified":"2024-11-04T04:26:28+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}Executing JavaScript Functions Returned from an Ajax Response
Your request involves retrieving a JavaScript function as part of an Ajax response and subsequently calling it to perform certain actions. To accomplish this, it's important to consider the following aspects:
Function Availability
Once the Ajax response containing the JavaScript function is inserted into the target DIV element, the function becomes available for execution on that page.
Function Declaration
The function declaration within the JavaScript code returned by the Ajax callback must be syntactically correct. This ensures the browser can interpret the declaration code.
Function Execution
Even though the function declaration is present in the DIV element, it remains unexecuted, and the browser is unaware of its existence. To call the function, you must evaluate its declaration code using the eval() function.
Example
The following code demonstrates how to evaluate the declaration code and make the function callable:
var newsc = ''; var e = document.getElementById('div1'); e.innerHTML = newsc; eval(document.getElementById('sc1').innerHTML);
This example inserts the JavaScript function declaration into a DIV element and evaluates it, making the go() function available for execution.
Execution Timing
It's important to note that the function is available for invocation only after the evaluation process is complete. The exact timing may vary depending on the Ajax implementation and response handling.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3