2013-11-25 122 views
0

我通过JS AJAX功能这样我可以通过php函数触发一个javascript函数吗?

function getData(dataSource,datasend) 
{ 

var XMLHttpRequestObject = false; 

if (window.XMLHttpRequest) { 
XMLHttpRequestObject = new XMLHttpRequest(); 
} else if (window.ActiveXObject) { 
XMLHttpRequestObject = new 
ActiveXObject("Microsoft.XMLHTTP"); 
} 

if(XMLHttpRequestObject) { 
XMLHttpRequestObject.open("POST", dataSource, true); 
XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
XMLHttpRequestObject.send('data='+escape(datasend)); 

XMLHttpRequestObject.onreadystatechange = function() 
{ 
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
     document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText; 
      //when the html has require data extecute this function 
      if(html has needed data for the function){ 
       foo(); 
      } 

    } 
} 

function foo(){ 
    //stuff to do with the html when loaded 
} 

获取HTML数据,据我做到这一点是由PHP函数生成的HTML。现在是否有办法触发生成的html数据中的foo()函数或发送可触发函数的数据的方式。由于

+0

要么你可以在Javascript中实现该函数foo,要么通过另一个AJAX调用将数据发送到PHP函数 –

+1

当然,你可以但肯定你不应该 –

+0

@ A.Wolff事情是当你发送js在ajax中它得到追加到DOM,但你怎么能触发或执行它后追加它 –

回答

0

您可以使用像,

在php文件

echo '<script>foo();</script>'; 

javscript,

function foo(){ 
    //stuff to do with the html when loaded 
    alert('test'); 
    } 
+0

它没有工作,或者它与mahmood的答案相同,但仍然想执行foo()不getData().. .. –

1

,你可以这样做:

<?php 

     echo "<script type='text/javascript'> function foo() 
{ alert('my home');} foo();</script>";echo "<script type='text/javascript'> foo()</scipt>"; 
    ?> 
+0

我已经试过这个,但它没有工作,但它被附加到DOM但没有警报: –

+0

如何在PHP中调用js函数?让我看看代码 –

+0

我很确定你的代码不警觉...你试过了吗?它只附加功能给dom .. –

0

假设PHP ,html,javascript在相同的文件

<?php 
$stuff_to_do = "anything"; 
$desicion_variable = ($access_db_or_anything)?"needed_foo":"dont_needed_foo"; 
..... 
?> 
<!-- html --> 
<script> 
function getData(dataSource,datasend) 
{ 
...... 
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
     document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText; 
      //when the html has require data extecute this function 
      <?php 
      // condition_html_has_needed_data_for_the_function 
      if ($desicion_variable == "needed_foo"){ 
       ?> 
       foo(); 
       <?php 
      } 
      ?> 
    } 
} 
function foo(){ 
    //stuff to do with the html when loaded 
} 
</script> 

,如果你有文件JS是独立的HTML和PHP的:page_example.php events.js page_example.php

<?php 
$stuff_to_do = "anything"; 
$desicion_variable = ($access_db_or_anything)?"needed_foo":"dont_needed_foo"; 
..... 
?> 
<body data-optionFoo="<?php echo $desicion_variable ?>"> 
    <!-- html stuff --> 
</body> 
<script src="events.js"></script> 

events.js

function getData(dataSource,datasend) 
{ 
    ..... 
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
     document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText; 
      //when the html has require data extecute this function 
      var objDiv = document.getElementById("pnlOpcion"); 
      if (objDiv.getAttribute('data-optionFoo') == "needed_foo"){ 
       foo(); 
      } 
    } 
} 
function foo(){ 
    //stuff to do with the html when loaded 
} 
0

我试图从php触发js函数,但是我仍然通过在html数据中提供一个虚拟div来实现它,并在成功时检查它是否存在并触发了这样的功能

XMLHttpRequestObject.onreadystatechange = function() 
{ 
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
     document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText; 
      //when the html has require data extecute this function 
      if($('#foo_div').length > 0){ 
       foo(); 
      } 

    } 
} 

function foo(){ 
    //stuff to do with the html when loaded 
    //remove div after everything finished 
    $('#foo_div').remove(); 
} 

我仍然不认为这是正确的做法。任何正确的答案,我将不胜感激。

相关问题