Pre-Process Javascript before downloading to client machine
I want to pre-process a javascript file before downloading it the client browser. I have done this successfully with a css file, but when I apply the same criteria to my javascript file it does not work.
Does anyone know how to use PHP to pre-process a .js file before downlo开发者_StackOverflowad?
Thanks
Create a file, let's say jsfile.js.php
:
header("Content-type: text/javascript");
ob_start("ob_gzhandler");
include('jsfile.js');
ob_flush();
Now instead of including .js file, just include this .php file.
Small edit (after I saw your comment): If is only about added variables, you can define them in html, just before including the js file:
var timestamp = "<?php echo time(); ?>
<script src="jsfile.js"></script>
This project is great to minify, join and set variables in JS (but apparently discontinued for now): https://code.google.com/p/jspp/
精彩评论