Hi everyone. We know that Javascript is compiled when the browser loads your webpage. However, sometimes you need to compile your script loaded by Ajax, how should you do? As default, the browser doesn’t compile the script responded by Ajax. If you use the function eval() to compile your script, the variable (defined in your Ajax response code) will be unavailable to call in your page. Thus I have a tip that may help you solve this issue. You can call the function below to compile your script loaded by Ajax.
// Compile your custom script
function compileJsAjax(yourScript){
var jsElement = document.createElement('script');
jsElement.type = 'text/javascript';
jsElement.text = yourScript;
var existedJs = document.getElementsByTagName('script')[0];
existedJs.parentNode.insertBefore(jsElement,existedJs);
}