简体   繁体   English

使用PHP即时缩小Javascript

[英]Minification of Javascript on the fly with PHP

I use PHP to remove extra spaces and lines on the fly for HTML, CSS and javascript:- At the start of the PHP file I use ob_start(); 我使用PHP即时删除HTML,CSS和javascript的多余空格和线条:-在PHP文件的开头,我使用ob_start();

At the end of the script this :- 在脚本末尾:

echo preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', ob_get_clean() );

But after this my javascript code doesn't work. 但是在此之后,我的javascript代码不起作用。 I use semi-colons and brackets properly with javascript, so I wonder what is wrong in this code ! 我在javascript中正确使用了分号和方括号,所以我想知道这段代码有什么问题! Here is a bit of my javascript, it works if I dont use the function to remove extra lines :- 这是我的Javascript的一部分,如果我不使用该功能删除多余的行,它将起作用:-

function delfile(fileid)
    {
      var div = "f_" + fileid;
      var location = "/delfile/" + fileid;
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange=function()  {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
                    {
                        var patt = /Deleted/;
                        var text = xmlhttp.responseText;
                        if(patt.test(text))
                        {
                            var elem = document.getElementById(div);
                            elem.parentNode.removeChild(elem);
                        }
                    else    
                        {
                            alert('Some error deleting that');
                        }

                }
                            }
      xmlhttp.open("POST", location, true);
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.send("");
  }

After minifying all code on the fly, this code doesn't work ! 快速缩减所有代码后,此代码将不起作用! It gives this error : Uncaught ReferenceError: show is not defined files:1onclick files:1 它给出此错误: Uncaught ReferenceError: show is not defined files:1onclick files:1

Minified Output : <script>function delfile(fileid) { var div = "f_" + fileid; var location = "/delfile/" + fileid; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var patt = /Deleted/; var text = xmlhttp.responseText; if(patt.test(text)) { var elem = document.getElementById(div); elem.parentNode.removeChild(elem); } else { alert('Some error deleting that'); } } } xmlhttp.open("POST", location, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(""); }</script> 缩小的输出: <script>function delfile(fileid) { var div = "f_" + fileid; var location = "/delfile/" + fileid; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var patt = /Deleted/; var text = xmlhttp.responseText; if(patt.test(text)) { var elem = document.getElementById(div); elem.parentNode.removeChild(elem); } else { alert('Some error deleting that'); } } } xmlhttp.open("POST", location, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(""); }</script> <script>function delfile(fileid) { var div = "f_" + fileid; var location = "/delfile/" + fileid; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var patt = /Deleted/; var text = xmlhttp.responseText; if(patt.test(text)) { var elem = document.getElementById(div); elem.parentNode.removeChild(elem); } else { alert('Some error deleting that'); } } } xmlhttp.open("POST", location, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(""); }</script>

I guess there is some problem near xmlhttp.onreadystatechange=function() { . 我猜xmlhttp.onreadystatechange=function() {附近有问题。

You're missing a semicolon after the xmlhttp.onreadystatechange so it should look like this xmlhttp.onreadystatechange之后,您缺少分号,因此它应该像这样

xmlhttp.onreadystatechange=function()  { ... };

Although the best advice I think is not to use such minification because you're risking unexpected errors (such as the one you're asking about). 尽管我认为最好的建议是不要使用这样的缩略词,因为您可能会遇到意想不到的错误(例如您要询问的错误)。 Also, I'm not sure if the few bytes you will safe are worth this extra hassle. 另外,我不确定您可以安全使用的几个字节是否值得这个额外的麻烦。

If you really have a good reason to minify your js/html, then I'd go for some reliable software/library to do this. 如果您确实有充分理由缩减js / html,那么我会去找一些可靠的软件/库来做到这一点。

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

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