You can make PHP and JavaScript work together by generating the following line in each page template:
Then you can refer to document.mybaseurl anywhere in your JavaScript. This saves you some debugging and complexity because this variable is always consistent with the PHP calculation.