[英]How to execute a javascript function that is in ajax call
There should be a simple solution this this issue but I am not being able to figure it out!! 这个问题应该有一个简单的解决方案,但我无法弄清楚!! So I am getting data/templates via ajax. 所以我通过ajax获取数据/模板。 And there are some function in the ajax data. 并且ajax数据中有一些功能。 When I try to call the function it throws and error. 当我尝试调用该函数时,它将引发错误。 Example in jsfiddle with regular vs ajax function calls. 带有常规vs.ajax函数调用的jsfiddle中的示例。 http://jsfiddle.net/rexonms/b05uxko6/ http://jsfiddle.net/rexonms/b05uxko6/
<!-- HTML -->
<div id="regularData">
<h2>Regular Data</h2>
<p onclick="mouseEvent()">Click Me</p>
<script>
function mouseEvent(){
alert('Yep clicked');
}
</script>
</div>
<hr>
<div id="ajaxData"></div>
<!-- Javascript -->
var request = new XMLHttpRequest(); // do we need this?
request.open("POST", "https://kvdevl06.sohalo.com/apps/kobie/php/widget/dispatcher_tpl.php");
request.onreadystatechange = function(){
if(request.readyState === 4){
alert('Ajax data is: ' + request.response);
document.getElementById("ajaxData").innerHTML = request.response;
}
}
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("action=getTest&partner_user_id=1&template_id:test");
You may check the following question: Executing <script> elements inserted with .innerHTML 您可能会检查以下问题: 执行插入.innerHTML的<script>元素
In short, you may iterate the script tags and eval
them. 简而言之,您可以迭代脚本标签并eval
它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.