我可以从php函数触发javascript函数吗?

时间:2013-11-25 10:59:01

标签: javascript php jquery ajax

我通过js ajax函数获取html数据

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数据中触发foo()函数或者发送可以触发函数的数据的方法。感谢

4 个答案:

答案 0 :(得分:1)

你可以这样做:

    <?php 

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

答案 1 :(得分:0)

你可以使用喜欢,

在php文件中, javascript,

答案 2 :(得分: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
}

答案 3 :(得分: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();
}

我仍然不认为这是正确的做法。任何正确的答案我都会非常感激。