I don't know much about JS, but if you were to have a dynamic document.write, you could just include PHP like this:
document.write('<? echo $var; ?>')
Basically just add <? & ?> anywhere you want to have PHP. Then again it might not work because the file extension isn't .php It always worth a try though 🙂