联系我们

  • 联系电话:13637710550
  • QQ 咨询QQ 467225336

当前位置: 首页 > ECshop经验共享 > ecshop中jQuery致命冲突解决方案

ecshop中jQuery致命冲突解决方案

主要就是Ecshop的AJAX传输类,transport.js 中重写了object的对象原型,从而导致了与jq框架的冲突。

解决:
1. 删除transport.js中587行 - 636行中关于object.prototype.toJSONString的定义
2. 自定义一个方法用于object对象的json序列化
如下

复制代码
  1. function obj2str(o)  
  2. {  
  3.   //开始  
  4.       var r = [];  
  5.    if(typeof o =="string"return "\""+o.replace(/([\'\"[url=file://\\])/g,]\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\[/url]"";  
  6.    if(typeof o =="undefined"return "undefined";  
  7.    if(typeof o == "object"){      
  8.     if(o===nullreturn "null";  
  9.     else if(!o.sort){  
  10.      for(var i in o)  
  11.      {    
  12.       if(i!="toJSONString"//增加判断,清除对object原型的定义加入到json中  
  13.       r.push("\""+i+"\""+":"+obj2str(o));  
  14.      }  
  15.      r="{"+r.join()+"}";  
  16.     }else{  
  17.      for(var i =0;i<o.length;i++)  
  18.       r.push(obj2str(o))  
  19.      r="["+r.join()+"]"  
  20.     }  
  21.     return r;  
  22.    }  
  23.    return o.toString();  
  24.   //结束     
  25. }   

3. 在模板页和js脚本中所有对于 obj.toJSONString()的地方,一概替换为obj2str(obj)

4. 重写好后发现compare.js, 主要重写其中的定时器 功能。 将以下代码替换到compare.js中

复制代码
  1. var Compare = new Object();  
  2. Compare = {  
  3.   add : function(goodsId, goodsName, type)  
  4.   {  
  5.     var count = 0;  
  6.     for (var k in this.data)  
  7.     {  
  8.       if (typeof(this.data[k]) == "function")  
  9.       continue;  
  10.       if (this.data[k].t != type) {  
  11.         alert(goods_type_different.replace("%s", goodsName));  
  12.         return;  
  13.       }  
  14.       count++;  
  15.     }  
  16.     if (this.data[goodsId])  
  17.     {  
  18.       alert(exist.replace("%s",goodsName));  
  19.       return;  
  20.     }  
  21.     else  
  22.     {  
  23.       this.data[goodsId] = {n:goodsName,t:type};  
  24.     }  
  25.     this.save();  
  26.     this.init();  
  27.   },  
  28.   init : function(){  
  29.     this.data = new Object();  
  30.     var cookieValue = document.getCookie("compareItems");  
  31.     if (cookieValue != null) {  
  32.       this.data = cookieValue.parseJSON();  
  33.     }  
  34.     if (!this.compareBox)  
  35.     {  
  36.       this.compareBox = document.createElement("DIV");  
  37.       var submitBtn = document.createElement("INPUT");  
  38.       this.compareList = document.createElement("UL");  
  39.       this.compareBox.id = "compareBox";  
  40.       this.compareBox.style.display = "none";  
  41.       this.compareBox.style.top = "200px";  
  42.       this.compareBox.align = "center";  
  43.       this.compareList.id = "compareList";  
  44.       submitBtn.type = "button";  
  45.       submitBtn.value = button_compare;  
  46.    this.compareBox.appendChild(this.compareList);  
  47.       this.compareBox.appendChild(submitBtn);  
  48.       submitBtn.onclick = function() {  
  49.         var cookieValue = document.getCookie("compareItems");  
  50.         var obj = cookieValue.parseJSON();  
  51.         var url = document.location.href;  
  52.         url = url.substring(0,url.lastIndexOf('/')+1) + "compare.php";  
  53.         var i = 0;  
  54.         for(var k in obj)  
  55.         {  
  56.           if(typeof(obj[k])=="function")  
  57.           continue;  
  58.           if(i==0)  
  59.             url += "?goods[]=" + k;  
  60.           else  
  61.             url += "&goods[]=" + k;  
  62.           i++;  
  63.         }  
  64.         if(i<2)  
  65.         {  
  66.           alert(compare_no_goods);  
  67.           return ;  
  68.         }  
  69.         document.location.href = url;  
  70.       }  
  71.       document.body.appendChild(this.compareBox);  
  72.     }  
  73.     this.compareList.innerHTML = "";  
  74.     var self = this;  
  75.     for (var key in this.data)  
  76.     {  
  77.       if(typeof(this.data[key]) == "function")  
  78.         continue;  
  79.       var li = document.createElement("LI");  
  80.       var span = document.createElement("SPAN");  
  81.       span.style.overflow = "hidden";  
  82.       span.style.width = "100px";  
  83.       span.style.height = "20px";  
  84.       span.style.display = "block";  
  85.       span.innerHTML = this.data[key].n;  
  86.       li.appendChild(span);  
  87.       li.style.listStyle = "none";  
  88.       var delBtn = document.createElement("IMG");  
  89.       delBtn.src = "themes/default/images/drop.gif";  
  90.       delBtn.className = key;  
  91.       delBtn.onclick = function(){  
  92.         document.getElementById("compareList").removeChild(this.parentNode);  
  93.         delete self.data[this.className];  
  94.         self.save();  
  95.         self.init();  
  96.       }  
  97.       li.insertBefore(delBtn,li.childNodes[0]);  
  98.       this.compareList.appendChild(li);  
  99.     }  
  100.     if (this.compareList.childNodes.length > 0)  
  101.     {  
  102.       this.compareBox.style.display = "";  
  103.       this.timer = window.setInterval("flowdiv('compareBox')", 50);  
  104.     }  
  105.     else  
  106.     {  
  107.       this.compareBox.style.display = "none";  
  108.       window.clearInterval(this.timer);  
  109.       this.timer = 0;  
  110.     }  
  111.   },  
  112.   save : function()  
  113.   {  
  114.     var date = new Date();  
  115.     date.setTime(date.getTime() + 99999999);  
  116.     document.setCookie("compareItems", obj2str(this.data));  
  117.   },  
  118.   lastScrollY : 0  
  119. }  
  120.   
  121. //用于定时器的自动滚动的层  
  122. lastScrollY=0;  
  123. function flowdiv(domid){  
  124.    var diffY;  
  125.      if (document.documentElement && document.documentElement.scrollTop)  
  126.       diffY = document.documentElement.scrollTop;  
  127.     else if (document.body)  
  128.       diffY = document.body.scrollTop  
  129.     else  
  130.       {/*Netscape stuff*/}  
  131.     //alert(diffY);  
  132.     percent=.1*(diffY-lastScrollY);   
  133.     if(percent>0) percent=Math.ceil(percent);   
  134.     else percent=Math.floor(percent);   
  135.      document.getElementById(domid).style.top=parseInt(document.getElementById(domid).style.top)+percent+"px";  
  136.     lastScrollY=lastScrollY+percent;   
  137.     //alert(lastScrollY);  
  138. }