最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Generate Javascript file with PHP - Stack Overflow

programmeradmin7浏览0评论

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=".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.

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./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.

Share Improve this question edited Apr 13, 2017 at 12:40 CommunityBot 11 silver badge asked Nov 5, 2012 at 10:41 Ben CareyBen Carey 17k20 gold badges93 silver badges181 bronze badges 4
  • 4 Nothing bad. Loading together speeds up your web application. You'd better hide your php file as js, this can be done with .htaccess – Bogdan Burym Commented Nov 5, 2012 at 10:44
  • @BogdanBurim Yeah thats what I am going to do :-) If I rewrite the js file to php, will that mean that I do not have to output headers from PHP? – Ben Carey Commented Nov 5, 2012 at 10:54
  • 1 No, headers still will be needed. For IE7 (as far as I remember) at least (he is stupid). – Bogdan Burym Commented Nov 5, 2012 at 11:02
  • @BogdanBurim Thought they might have been :-) – Ben Carey Commented Nov 5, 2012 at 11:11
Add a ment  | 

1 Answer 1

Reset to default 7

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 e up with caching methods as well to reduce the processing impact.

发布评论

评论列表(0)

  1. 暂无评论