简体   繁体   English

在外部php文件中包含缩小的javascript

[英]Including minified javascript in external php file

Basic question: 基本问题:

How to include a script.min.js in an external php file. 如何在外部php文件中包含script.min.js

My current setup: 我当前的设置:

index.php 的index.php

<script type="text/javascript" src="http://localhost/js.php"></script>

js.php 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";
?>

Long Question: 长问题:

I've ran into this issue a couple times over the years and ignored it by including it as a separate <script src=""></script> but it's time to find out if there is a solution. 多年来,我曾多次遇到此问题,并通过将其作为单独的<script src=""></script>包括在内而忽略了它,但现在该找出是否有解决方案了。

Occasionally I'll run into a javascript file that doesn't work when included. 有时,我会遇到一个JavaScript文件,该文件在包含时不起作用。 Just produces javascript errors. 只会产生JavaScript错误。 (I've tried echo file_get_contents(...) also) (我也试过echo file_get_contents(...))

My current script is tinymce.min.js . 我当前的脚本是tinymce.min.js As above it works fine if included <script src="tinymce.min.js"></script> after my js.php file. 如上所述,如果在我的js.php文件之后包含<script src="tinymce.min.js"></script> ,则可以js.php But included in the php file does not work. 但是包含在php文件中不起作用。

Shouldn't be relavent as it's not the first minified script to not work. 不应该大惊小怪,因为它不是第一个缩小的脚本,无法正常工作。 But the current error recieved in Firefox console is TypeError: c is not a constructor 但是,Firefox控制台中当前收到的错误是TypeError: c is not a constructor

Side question, should I be using Content-type: application/javascript since I'm only catering for modern browsers? 附带的问题是,因为我只适合现代浏览器,所以我应该使用Content-type: application/javascript吗?

With grunt you can easyly concat your js files and run the command a concat setting is like this 使用grunt,您可以轻松地连接您的js文件并运行命令,连接设置如下所示

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'
  }
}

so you can define your files you want to concat and also the destination 因此您可以定义要连接的文件以及目标位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM