0

I am getting html data through js ajax function like this

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
}

as far as i do it the html is generated by a php function. Now Is there way to trigger the foo() function in the html data generated or a way to send data that can trigger the function. Thanks

Sathya Raj
  • 1,079
  • 2
  • 12
  • 30
  • Either you can implement that function foo in Javascript or send the data to PHP function via another AJAX call – Prashant Singh Nov 25 '13 at 11:02
  • 1
    Sure you can but more surely you shouldn't – A. Wolff Nov 25 '13 at 11:05
  • @A.Wolff the thing is when you send js in a ajax it get appended to the dom but how can you trigger or execute it after you append it – Sathya Raj Nov 25 '13 at 11:17
  • @SathyaRaj using any ajax callback or method relative to returned promise interface http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – A. Wolff Nov 25 '13 at 11:28
  • @A.Wolff i looked at your q&a but it was about handling sync and async code i well aware of that and thats y i need something to send from php that could recognized in success function. so that i can call the foo() function . bcoz its an old code with html data is big i'd have to find without string manipulation .... – Sathya Raj Nov 25 '13 at 12:33

4 Answers4

1

you can do like this :

    <?php 

        echo "<script type='text/javascript'> function foo()
{ alert('my home');} foo();</script>";echo "<script type='text/javascript'> foo()</scipt>";
    ?>
Mahmood Rehman
  • 4,303
  • 7
  • 39
  • 76
0

You can use like,

in php file,

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

javscript,

  function foo(){
     //stuff to do with the html when loaded
     alert('test');
  }
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

assuming that php,html,javascript is in the same file

<?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>

and if you have file js is separate of html and 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
}
jvinces
  • 21
  • 3
0

I tried to trigger the js function from php but in vein but i have achieve it by giving a dummy div in html data and checked it in success if present and triggered the function like this

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();
}

I still don't think that this is the properway to do it. Any Proper answer i'll greatly appreciate it.

Sathya Raj
  • 1,079
  • 2
  • 12
  • 30