检测 javascript 是否使用 Javascript 中的 YIU 压缩器进行压缩
我做了一个延迟加载 javascript 的构造,以加快页面加载速度。为了部署网站,我使用 YIU 压缩器来最小化脚本,同时也是为了提高速度。脚本将有两种版本,一种是压缩版本,另一种是原始版本。例如:example-min.js 和 example.js。有些 javascript 仅在功能出现时才会加载,有时需要更多脚本(插件想法)。
不管怎样,我想知道(如果需要其他js文件)“主机”js文件是否被压缩,所以当它被压缩时,它会加载js文件的缩小版本。如果没有(在开发时),它将加载原始文件。
例如:
function myObject()
{
var o = this;
o.isJsMinified = function()
{ s = o.isJsMinified.toString(),ss='s= ';
return (s.indexOf(ss+'o.is')<0);
};
o.require = function( s, delay, baSync, fCallback ) // add script to dom
{
var oo = document.createElement('script'),
oHead = document.getElementsByTagName('head')[0],
f = (typeof fCallback == 'function')?fCallback:function(){};
if( !oHead )
{ return false; }
oo.onload = function() {oo.id='loaded'; f(oo); };
oo.type = 'text/javascript';
oo.async = (typeof baSync == 'boolean')?baSync:false;
oo.src = s;
setTimeout( function() { oHead.appendChild(oo); }, (typeof delay != 'number')?delay:1 );
return true;
};
.....
.....
if( <new functionality found> )
{
if( o.isJsMinified() )
{ o.require('new-min.js',800); }
else { o.require('new.js',800); }
}
.....
.....
}
当您查看 jsIsMinified() 函数时,我使用一个技巧来检测函数本身(源)是否从空格中删除(缩小版本)。但是,Firefox 有一个问题,它不会返回原始格式,因此无法检测到任何差异。
例如: // 由 YUI 压缩器压缩:
o.isJsMinified=function(){s=o.isJsMinified.toString(),ss='s= ';return (s.indexOf(ss+'o.is')<0);};
Firefox 将显示:
function () {
s = o.isJsMinified.toString(), ss = "s= ";
return s.indexOf(ss + "o.is") < 0;
}
该函数在 Firefox 中失败,它总是“说”它没有被压缩。有人知道这个问题的解决方法吗?
I have made a construction to defer loading javascript to speed up page loading. To deploy websites i use the YIU compressor to minimize the scripts, also for speed. There will be two versions of the scripts, one is compressed and orginal one is not. For example: example-min.js and example.js. Some javascripts will be loaded only when a functionality comes in and sometimes it needs more scripts (plugin idea).
Anyway, i want to know (if other js files are needed) if the 'host' js file is compressed, so when it is compressed it loads the minified version of the js file. If not (when developing) it loads the orginial file.
For example:
function myObject()
{
var o = this;
o.isJsMinified = function()
{ s = o.isJsMinified.toString(),ss='s= ';
return (s.indexOf(ss+'o.is')<0);
};
o.require = function( s, delay, baSync, fCallback ) // add script to dom
{
var oo = document.createElement('script'),
oHead = document.getElementsByTagName('head')[0],
f = (typeof fCallback == 'function')?fCallback:function(){};
if( !oHead )
{ return false; }
oo.onload = function() {oo.id='loaded'; f(oo); };
oo.type = 'text/javascript';
oo.async = (typeof baSync == 'boolean')?baSync:false;
oo.src = s;
setTimeout( function() { oHead.appendChild(oo); }, (typeof delay != 'number')?delay:1 );
return true;
};
.....
.....
if( <new functionality found> )
{
if( o.isJsMinified() )
{ o.require('new-min.js',800); }
else { o.require('new.js',800); }
}
.....
.....
}
When you look at the jsIsMinified() function i use a trick to detect if the function itself (the source) is stripped from spaces (minified version). But, there is a problem with firefox, it does not return the original formatting so it cannot detect any difference.
For example:
// compressed by YUI compressor:
o.isJsMinified=function(){s=o.isJsMinified.toString(),ss='s= ';return (s.indexOf(ss+'o.is')<0);};
Firefox will display:
function () {
s = o.isJsMinified.toString(), ss = "s= ";
return s.indexOf(ss + "o.is") < 0;
}
The function fails in firefox, it always 'says' it is not compressed. Does anybody know a work-around for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的认为你把事情搞得太复杂了。哇哦,为什么不在服务器上创建两个文件夹,例如
js/product
和js/debug
。您的 JavaScript 文件中可能有一个标志,可以指示它是否有效或调试。想一想,那些文件夹也是不必要的。您可以只区分
.min
与否。您真正需要做的唯一一件事就是在代码中设置一个全局DEBUG
变量。I really think you overcomplicate things here. Woah, why don't you just create two folders on your server like
js/production
andjs/debug
. You might have one flag within your javascript files that could indicate whether or not it's productive or debug. Likethinking about that, even those folders are unnecessary. You can just distinguish between
.min
or not. The only thing you really need to do is to set a globalDEBUG
variable in your code.HTTP 标头将指定是否请求并启用压缩,但是没有简单的方法来读取它们 因此您可能需要一个特殊的 Firefox 扩展来进行开发。
也许进行更改的正确位置是在服务器中,如果给出了某个标头(并且请求者 IP 地址是本地的),则发送未缩小的脚本。
The HTTP headers will specify whether compression was requested and enabled, but there's no easy way to read them so you might want a special Firefox extension for development.
Perhaps the right place to make the change is in the server, send the non-minified script out if a certain header is given (and the requestor IP address is local).