2017-02-09 55 views
0

我使用ajax縮小器來減少代碼請求和HTML開銷,到目前爲止它的CSS文件工作正常。但是當我嘗試使用js文件的縮小輸出時,我的HTML頁面停止工作。Ajax Minifier在縮小時打破JavaScript

這裏有

<script src="/content/template/js/AuswellScript/jquery-1.11.0.min-f1cd7227-4b48-423d-ba00-6a814fc588e3.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/bootstrap.min-54c76ae6-1272-464d-b3b4-e5c8e13cd2ad.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/own-menu-d7c1ca2a-6a7e-4bc3-b7e9-f51e92136f20.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/owl.carousel.min-5ea2a8c2-1a05-4f29-aca3-b71f936aaa25.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.superslides-d8526db0-813c-4372-9699-3c829e696ccc.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/masonry.pkgd.min-8f6e02f7-17a3-442f-a7db-a2f68ec24cb2.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.stellar.min-f9b2d588-2c90-489a-ab22-800f41f7df17.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery-ui-1.10.3.custom-0406d77e-4eed-4e42-b1ef-370e24378d13.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.magnific-popup.min-7e2779cb-0844-4b67-acae-2a1053cb0401.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.isotope.min-0148ab04-7187-43c6-accf-7d7ffd4224fa.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.flexslider-min-ec6844aa-338c-4ce9-a67c-eaff0948809d.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/appointment-a7ff2a91-9cfb-4dc9-95a0-585033435e12.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/Slider-568c53bd-f8a3-440a-b2d4-1969699f1d7b.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.downCount-8ba17b7f-82ca-4b65-a29e-517b3e3d51af.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/counter-08aa3c8c-b65f-4efb-842b-dbbd019a11ce.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/jquery.prettyPhoto-d3012702-c0cc-439a-8b7e-22e0d2211a15.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/main-4310f5f3-278c-463e-bc98-edbbc307fbe9.js" type="text/javascript"></script> 
<script src="/content/template/js/AuswellScript/bootbox.min-8eea52d3-baa1-47fd-b4df-5be51462a2e9.js" type="text/javascript"></script> 

的生成合並文件被保存爲

<script src="http://auswell2.xoxo-it.com/content/template/js/AuswellScript/combined.min.js" type="text/javascript"></script> 

,但使用該文件對單獨的文件導致沒有定義的錯誤$的js文件我包括組合輸出我的頁面,我刪除並重新創建了幾次,以確保沒有順序或不正確的數據造成問題。

這裏是C#代碼

Minifier min = new Minifier(); 
StringBuilder minified = new StringBuilder(); 
script.head_files.ForEach(js => 
{ 
    if (!string.IsNullOrWhiteSpace(js)) 
     minified.Append(min.MinifyJavaScript(System.IO.File.ReadAllText(Server.MapPath(js)))); 
}); 
if (minified.Length > 0) 
{ 
    var minJS = Server.MapPath("~/content/template/js/" + objHeads.head_name + "/combined.min.js"); 
    System.IO.File.WriteAllText(minJS, minified.ToString()); 
} 

回答

0

我貼在的jsfiddle代碼,點擊「整潔」,然後複製整理輸出通天REPL ...好像這是一個很大的地方是缺少一個; - 可能在輸入文件之間「有一種方法可以讓縮小器指定要將;添加到每個輸入文件的開頭(或結尾)?

之後或許minified.Append(min.MinifyJavaScript(System.IO.File.ReadAllText(Server.MapPath(js))));

即添加minified.Append(";");

Minifier min = new Minifier(); 
StringBuilder minified = new StringBuilder(); 
script.head_files.ForEach(js => { 
    if (!string.IsNullOrWhiteSpace(js)) { 
     minified.Append(min.MinifyJavaScript(System.IO.File.ReadAllText(Server.MapPath(js)))); 
     minified.Append(";"); // add this 
    } 
}); 
if (minified.Length > 0) 
{ 
    var minJS = Server.MapPath("~/content/template/js/" + objHeads.head_name + "/combined.min.js"); 
    System.IO.File.WriteAllText(minJS, minified.ToString()); 
} 
+0

感謝我要去試試這個,讓你知道 – Alok

+0

它的工作非常感謝您的信息,標記爲答案:) – Alok