I have a JavaScript which is currently being re-used in over 5 websites, and will prolly be used by alot more websites as time progresses, the thing is – that javascript runs some checks according to the server name, and I was wondering what’s the best way to create some JS file which has some server side variables in it, such as:
js-functions.php:
<script type='text/javascript'>
var myServer = <?php echo $_SERVER['SERVER_NAME'] ?>;
</script>
as currently, this file will be downloaded every time, so how can I make it send out a 304 Unmodified, and use the browser caching to my advantage
I would not create a dynamic JS file at all. If at all possible, put all the dynamic stuff into the main document; then load the main chunk of JavaScript from a static resource.
Put the code you already have into the
headsection of each HTML page:then link to a static JavaScript file:
inside the JavaScript file, do not use any PHP; use the
myServervariable to do your checks.The advantage of this is that if the web server is configured correctly, the static JS file will be loaded only once and you don’t have to worry about caching.
You could even share the same JavaScript URL between all 5 sites.