简体   繁体   English

合并多个jquery文件

[英]Combining multiple jquery files

For some reason I can't combine my javascript files in one single file through php, like I can with css. 由于某种原因,我无法通过php将我的javascript文件合并为一个文件,就像使用CSS一样。

Basicly I wish to combine all of my javascript files into one. 基本上,我希望将我所有的javascript文件合并为一个。 However that breaks my code somehow. 但是,这以某种方式破坏了我的代码。 It's like the $-value which jQuery generates isn't available through the rest of the code (such as jquery ui). 就像jQuery生成的$值在其余代码(例如jquery ui)中不可用。

Does the jQuery need to be loaded in it's own <script type="text/javascript" src=""></script> for some reason? 出于某种原因,是否需要将jQuery加载到自己的<script type="text/javascript" src=""></script>中? And what is that reason then? 那是什么原因呢?

I include them in the same order that they work in the browser. 我按照它们在浏览器中的工作顺序将它们包括在内。

jQuery -> jQuery UI -> jQuery custom functions -> my own script jQuery-> jQuery UI-> jQuery自定义函数->我自己的脚本

You might want to put a new-line and/or semicolon between the single scripts. 您可能需要在单个脚本之间放置换行符和/或分号。
Eg the minified jquery ui script you get from https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js ends with 例如,您从https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js获得的缩小的jQuery UI脚本以

return this}})}(jQuery)

If you now append another script right behind that you will most likely get a syntax error. 如果现在在其后追加另一个脚本,则很可能会收到语法错误。
Lets take a crude scripts.php 让我们看一下原始的scripts.php

<?php
header('Content-type: Application/javascript');
readfile('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
readfile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js');

echo '$(document).ready(function() {
    alert("hello");
    $( "#dialog" ).dialog( "open" );
});
';

this won't work, but 这行不通,但是

<?php
header('Content-type: Application/javascript');
readfile('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
echo "\r\n;";
readfile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js');
echo "\r\n;";    
echo '$(document).ready(function() {
    alert("hello");
    $( "#dialog" ).dialog( "open" );
});
';

will. 将。

Other things to consider: 要考虑的其他事项:

  • this is probably not a silver bullet. 这可能不是灵丹妙药。 I chose "\\r\\n;" 我选择了“ \\ r \\ n;” to a) escape from a // comment in the last line and b) to close an "open" statement with the semi-colon. a)从最后一行的//注释中退出,并b)用分号关闭“打开”语句。 But the missing-semicolon behaviour of javascript (and the implementations of it) can be rather odd (it's a bad feature) and I'd rather "fix" each individual script manually than adding a fixed "repair string". 但是javascript(及其实现)缺少分号的行为可能很奇怪(这是一个坏功能),我宁愿手动“修复”每个脚本而不是添加固定的“修复字符串”。
  • I'm stronlgy in the "let a cdn like google host those javascript libraries and rely on the client's cache" camp ...but without good evidence at hand to support that position. 在“让像Google这样的CDN托管那些javascript库并依靠客户端的缓存”这一阵营中,我很坚定……但是手头没有很好的证据来支持这一立场。

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

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