2013-11-25 181 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(); 
} 

我仍然不認爲這是正確的做法。任何正確的答案,我將不勝感激。

相關問題