Dynamically generate javascript using php
I need to dynamically generate and execute some javascript on change of a select box. Right now, I am using a remote php file to generate that javascript (using mysql qu开发者_StackOverfloweries).
I don't know how then to run that javascript in my main page again. I've been playing around with .getscript, but I have no idea if I am heading in the right direction. I'm very new to all of this.
Right now, I am simply using:
$.getScript(url.php);
to call my php file.
My php file produces something like:
$(function() {
$( "#dateStartMainChartSelect" ).datepicker({
minDate: new Date(2011,03,07)),
maxDate: +0
});
});
Try to change your PHP to generate a function instead of a "DomReady" script, like this:
function updatephp() {
$("#dateStartMainChartSelect").datepicker({
minDate: new Date(2011,03,07),
maxDate: +0
});
}
After that you can use the callback function of getScript
to start your new function:
$.getScript('ajax/test.js', function() { updatephp(); });
精彩评论