简体   繁体   English

ajax加载PHP文件后无法执行javascript命令

[英]javascript commands not executing after ajax loads PHP file

I have a PHP script being executed by AJAX, the PHP script loads everything fine except for js scripts in the actual PHP file which I am using to close divs(if the PHP file isnt run via AJAX all the scripts work FYI).. 我有一个由AJAX执行的PHP脚本,除了我用来关闭div的实际PHP文件中的js脚本外,该PHP脚本都可以正常运行(如果PHP文件不是通过AJAX运行的,则所有脚本都可以使用FYI)。

this is the script not being run in the PHP file by ajax 这是ajax不在PHP文件中运行的脚本

 echo "<script> closeOne('" . $postid . "'); </script>";

The JS scripts to close divs are included in the header of the page 关闭div的JS脚本包含在页面的标题中

This is the AJAX script I am using 这是我正在使用的AJAX脚本

<script type="text/javascript">
function mainload(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest(str);
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("loadmain").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","mainload.php?page="+str,true);
    xmlhttp.send();
}
</script>

Any help would be appreciated. 任何帮助,将不胜感激。

JS in the ajax response is not executed by default. 默认情况下,不执行ajax响应中的JS。 You have to parse and identify JS statements, then run them using eval function of js. 您必须解析并标识JS语句,然后使用js的eval函数运行它们。 See this code as an example to start your own code: 请参阅以下代码作为示例以启动自己的代码:

var sc=xmlhttp.responseText;            
sc=sc.replace(/[\n\r\t\v\u00A0\u2028\u2029]{1,}/gm, ''); //Remove all types of White Space
sc=sc.match(/<.*?script.*?>.*?<\/.*?script.*>/gm); //divide into array, all the script tags

Then you will loop each of the 'sc' and run the following in the loop: 然后,您将循环每个“ sc”并在循环中运行以下命令:

abc=loop_var.replace(/<.*?script.*?>(.*?)<\/script>/gm, "$1"); //remove script/open close tags and get JS only
eval(abc);

If you add script element to innerHTML of a DOM element, JS wont run immediately. 如果将脚本元素添加到DOM元素的innerHTML中,JS将不会立即运行。 because that script element is just added. 因为刚刚添加了该脚本元素。

and that tag is not going to get parsed at all. 并且该标记根本不会被解析。

Ideally you should send a response in form of a JSON wrapped function. 理想情况下,您应该以JSON包装函数的形式发送响应。

You can send output like this from the PHP page. 您可以从PHP页面发送这样的输出。

echo "(function(){ closeOne('" . $postid . "'); })";

In response, you could evaluate this String containing the function using eval. 作为响应,您可以使用eval评估包含函数的此String。

 codeToExecute = eval(xmlhttp.responseText);

then call the function, 然后调用该函数,

 codeToExecute();

Note : sending functions from server and directly executing it using eval could be a security concern. 注意 :从服务器发送函数并使用eval直接执行它可能是出于安全考虑。 be careful and better use some JS libraray function to evaluate the string into a JSON object. 请小心,最好使用一些JS libraray函数将字符串评估为JSON对象。

There will be String-To-JSON convertersin jQuery,prototype and sencha's libraraies. jQuery,原型和sencha的库中将有String-To-JSON转换器。 Better use them or if the browser supports, JSON.parse is also a good option. 最好使用它们,或者如果浏览器支持,JSON.parse也是一个不错的选择。

But I hope the concept is clear to you. 但我希望这个概念对您很清楚。

Alternatively , You could just send the post id from server in a JSON and call closeOne on the response like this: 或者 ,您可以仅以JSON格式从服务器发送帖子ID,并在响应中调用closeOne,如下所示:

in PHP, 在PHP中,

echo "{post_id:".$postid."}";

and when the response comes, 当回应来临时,

responseObject =  eval("("+xmlhttp.responseText+")");

closeOne(responseObject.post_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM