简体   繁体   中英

Need to Execute Javascript in Ajax Call

The following code works in Chrome and Firefox. But I'm having trouble with Safari.

Main.php contains:

<script>
function show<?php echo $s; ?>(str) {
if (str.length == 0) { 
    document.getElementById("textOrder").innerHTML = "";

    return;
} else {

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("textOrder").innerHTML =       this.responseText;

             $.post("<?php echo site_url(); ?>/test.php?q=" + str, function(response){




                   $("#ajax_responses").load(response);
$("#ajax_responses").html(response);

     var quantity = $('#quantity',response).val();
                     document.getElementById('example').value = quantity;

$("#ajax_responses").find("script")(function(){
 eval($(this).text());
 });
   });

         }
     };
    xmlhttp.open("GET", "<?php echo site_url(); ?>/test.php?q=" + str, true);
    xmlhttp.send();

  }
}




</script>
<div id="ajax_responses" style="display:none;"></div>

I have Javascript inside of my AJAX response file that I'm trying to execute. The code executes in Chrome and Firefox.

My test.php contains the following

<script type="text/javascript" id="runscript">


    alert("test");

</script>

How can I get the Javascript to execute in Safari?

Any help will be greatly appreciated!

Thanks!

Really surprised no one provided any answers... However, I found a solution to the problem.

You can echo the javascript with PHP in the Ajax call.

Something like this:

 <?php echo '<script>alert("test")</script>'; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM