用 PHP 生成 Javascript 文件 [英] Generate Javascript file with PHP

查看:39
本文介绍了用 PHP 生成 Javascript 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始编写一个 API,它使用 PHP 加载、缩小和返回 javascript 文件到一个文件中.这是通过从 HMTL 中的 script 标记指向 PHP 文件来实现的,如下所示:

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>

这个 jsapi.php 页面处理 javascript 文件并输出缩小的 javascript,带有标题:

This jsapi.php page processes the javascript files and outputs the minified javascript, with the header:

header("Content-Type: text/javascript");

我的问题

这是加载 javascript 文件的错误方法吗?通过简单地指向 src 属性中的 .js 文件来单独加载 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?

如果您想查看我的完整 API 代码,请查看 这个.提到的链接还详细解释了我在做什么以及为什么.

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.

推荐答案

比 .js 更快,是的,但略有不足.原因是它将是一个静态文件,因此不需要 PHP 所需的处理时间.

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.

也就是说,像这样通过 PHP 提供 JavaScript 并没有错.您甚至可以想出缓存方法来减少处理影响.

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.

这篇关于用 PHP 生成 Javascript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆