簡體   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