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

5
  • Either you can implement that function foo in Javascript or send the data to PHP function via another AJAX call Commented Nov 25, 2013 at 11:02
  • 1
    Sure you can but more surely you shouldn't Commented Nov 25, 2013 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 Commented Nov 25, 2013 at 11:17
  • @SathyaRaj using any ajax callback or method relative to returned promise interface stackoverflow.com/questions/14220321/… Commented Nov 25, 2013 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 .... Commented Nov 25, 2013 at 12:33

4 Answers 4

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>";
    ?>
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this but it didn't work but its got appended to the dom but no alert :|
i m pretty sure your code is not alert ... have you tried it. it only appends the function to dom ..
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
}

Comments

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

1 Comment

it didn't work either its same as mahmood's answer but still i want to execute foo() not getData() ....
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.