To include raw JavaScript files into my pages, i've ended up using:
function include_js($jsfile, $basepath = JSPATH){
echo '<script type="text/javascript">';
include($basepath . $jsfile);
echo '</script>';
}
Works fine, and the PHP code inside these JS files is still executing fine, no problem here. But here is some pseudo code of what i used before:
<script>
var hello = '<?php echo $id; ?>';
</script>
So, here's the problem:
- Before, the PHP code used inside my JavaScript files was executed in the same context as the page's one.
- Now, it's executed in the context of the
include_js()
function.
Thus, i don't have access to any of my page's variables anymore. I could fix it with a global $id;
, but that was pseudo-code.
Actually, i have NO idea what variables i'll need to have access to.
Any idea how to solve that problem? If you have a better solution than what i'm actually doing inside include_js()
to achieve the same goal without the problem i'm talking about, that would be as much appreciated!