繁体   English   中英

使用PHP即时缩小Javascript

[英]Minification of Javascript on the fly with PHP

我使用PHP即时删除HTML,CSS和javascript的多余空格和线条:-在PHP文件的开头,我使用ob_start();

在脚本末尾:

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

但是在此之后,我的javascript代码不起作用。 我在javascript中正确使用了分号和方括号,所以我想知道这段代码有什么问题! 这是我的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("");
  }

快速缩减所有代码后,此代码将不起作用! 它给出此错误: Uncaught ReferenceError: show is not defined files:1onclick files:1

缩小的输出: <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>

我猜xmlhttp.onreadystatechange=function() {附近有问题。

xmlhttp.onreadystatechange之后,您缺少分号,因此它应该像这样

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

尽管我认为最好的建议是不要使用这样的缩略词,因为您可能会遇到意想不到的错误(例如您要询问的错误)。 另外,我不确定您可以安全使用的几个字节是否值得这个额外的麻烦。

如果您确实有充分理由缩减js / html,那么我会去找一些可靠的软件/库来做到这一点。

暂无
暂无

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

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