问题
My current code
I have started writing an API that loads, minifies and returns javascript files into one file using PHP. This is achieved by pointing to a PHP file from a script
tag in HMTL like so:
<script type="text/javascript" src="https://libraries.sinemaculammviii.com/jsapi.php"></script>
This jsapi.php
page processes the javascript files and outputs the minified javascript, with the header:
header("Content-Type: text/javascript");
My Question
Is this a bad method to load javascript files? Would it be much faster and reliable to load the javascript files individually by simply pointing to the .js
file in the src
attribute?
If you wish to see my code for the the full API, have a look at this. The mentioned link also explains in detail what it is I am doing and why.
回答1:
Faster as .js yes, but marginally. The reason being that it would be a static file so wouldn't need the processing time PHP will take.
That said there's nothing wrong with feeding JavaScript through PHP like this. You could even come up with caching methods as well to reduce the processing impact.
来源:https://stackoverflow.com/questions/13230411/generate-javascript-file-with-php