基本问题:
如何在外部 php 文件中包含script.min.js
。
我目前的设置:
索引.php
<script type="text/javascript" src="http://localhost/js.php"></script>
JS.php
<?php
header('Content-type: text/javascript');
include "script1.js"; echo "'n'n";
include "script2.js"; echo "'n'n";
include "script3.js"; echo "'n'n";
?>
长问题:
多年来,我多次遇到这个问题,并通过将其作为单独的<script src=""></script>
来忽略它,但现在是时候找出是否有解决方案了。
偶尔我会遇到一个 javascript 文件,该文件在包含时不起作用。只是产生JavaScript错误。(我也尝试过回声file_get_contents(...)
我目前的脚本是tinymce.min.js
。如上所述,如果包含在我的js.php
文件之后<script src="tinymce.min.js"></script>
,它可以正常工作。但是包含在 php 文件中不起作用。
不应该是相关的,因为它不是第一个不起作用的缩小脚本。但是当前在Firefox控制台中收到的错误是TypeError: c is not a constructor
附带问题,我应该使用Content-type: application/javascript
,因为我只迎合现代浏览器?
使用 grunt,您可以轻松连接您的 js 文件并运行命令 concat 设置是这样的
concat: {
options: {
// define a string to put between each file in the concatenated output
separator: ';'
},
dist: {
// the files to concatenate
src: ['src/**/*.js'],
// the location of the resulting JS file
dest: 'dist/<%= pkg.name %>.js'
}
}
因此,您可以定义要连接的文件以及目标