开发者

Pre-Process Javascript before downloading to client machine

开发者 https://www.devze.com 2023-03-05 08:30 出处:网络
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 wo

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号