dtkf64283 2014-10-17 13:50
浏览 19
已采纳

使用PHP动态缩小Javascript

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();

At the end of the script this :-

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

But after this my javascript code doesn't work. I use semi-colons and brackets properly with javascript, so I wonder what is wrong in this code ! Here is a bit of my javascript, it works if I dont use the function to remove extra lines :-

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

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>

I guess there is some problem near xmlhttp.onreadystatechange=function() {.

  • 写回答

1条回答 默认 最新

  • duanke3985 2014-10-17 14:13
    关注

    You're missing a semicolon after the xmlhttp.onreadystatechange so it should look like this

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统