简体   繁体   中英

Call a JavaScript function after an Ajax request

I have a problem with executing a JavaScript function after an Ajax request with JQuery.

First, I call a PHP page with an Ajax request, everything works fine.

In this PHP page, I return a JavaScript call for a function.

But when I append this script on my HTML page, Firebug return this : "ReferenceError: getNumber is not defined" getNumber(55); . (55 is just an example, it can be from 0 to 100)

HTML (Jquery and myScript are loaded correctly):

<script src="jquery1111min.js"></script>
<script src="myScript.js"></script>
<div id="content_modal" class="content_modal">

</div>

JQuery (myScript.js):

$.get("inc/get_something.php", function(data) {
  $(".content_modal").html(data);//Works
});
function getNumber(nb) {
  alert(nb);//never called
}

PHP (get_something.php):

//some html
$number = mt_rand(0,100);
<script>getNumber(<?=$number?>);</script>

Cheers

Is your

function getNumber(nb) { ... } 

oustide your

$(document).ready()

or

$(window).load()

If not, try putting it outside anything, in top of your tag

You cannot execute javascript from a php page that isn't rendered in the browser. JavaScript is rendered in the browser and not on the server.

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