(Rhetorical Question)
I ran into a bizarre scenario today where I wanted PHP to update how the javascript behaved on-the-fly. It was irritating, here is what I tried...
/*
* ajax-php-javascript-function-loader.php
*
* this gets called by AJAX and defines or re-defines the
* definition of dynamicDoStuff()
*/
<script type="text/javascript">
function dynamicDoStuff(a,b){
<?php
//dynamically defined function behavior
?>
}
</script>
This did not work because when the new javascript was loaded, it's scope of the new function definition was limited to the new script tags. Scripts elsewhere on the page couldn't read it.
So here is what you have to do.
/*
* index.html
*/
<html>
<head>
<script type="text/javascript">
var dynamicDoStuff;
</script>
<!-- Other Head Stuff -->
</head>
<body>
<!-- the body of the site -->
</body>
</html>
and
/*
* axax-php-javascript-function-loader.php
*/
<script type="text/javascript">
dynamicDoStuff = function(a,b){
<?php
//dynamically define function behavior
?>
}
</script>
by defining the name of the function in the header it becomes globally accessible so you can re-purpose it dynamically using ajax and (php or whatever)