拆分问题Internet Explorer

时间:2011-07-23 20:26:20

标签: php javascript css wordpress disqus

之前有关此错误的问题。

错误:无法获取属性“split”的值:object为null或undefined

提供了一个答案来添加以下代码:

/* Cross-Browser Split 1.0.1
(c) Steven Levithan <stevenlevithan.com>; MIT License
An ECMA-compliant, uniform cross-browser split method */

var cbSplit;

// avoid running twice, which would break `cbSplit._nativeSplit`'s reference to the native `split`
if (!cbSplit) {

cbSplit = function (str, separator, limit) {
    // if `separator` is not a regex, use the native `split`
    if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
        return cbSplit._nativeSplit.call(str, separator, limit);
    }

    var output = [],
        lastLastIndex = 0,
        flags = (separator.ignoreCase ? "i" : "") +
                (separator.multiline  ? "m" : "") +
                (separator.sticky     ? "y" : ""),
        separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy
        separator2, match, lastIndex, lastLength;

    str = str + ""; // type conversion
    if (!cbSplit._compliantExecNpcg) {
        separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt
    }

    /* behavior for `limit`: if it's...
    - `undefined`: no limit.
    - `NaN` or zero: return an empty array.
    - a positive number: use `Math.floor(limit)`.
    - a negative number: no limit.
    - other: type-convert, then use the above rules. */
    if (limit === undefined || +limit < 0) {
        limit = Infinity;
    } else {
        limit = Math.floor(+limit);
        if (!limit) {
            return [];
        }
    }

    while (match = separator.exec(str)) {
        lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser

        if (lastIndex > lastLastIndex) {
            output.push(str.slice(lastLastIndex, match.index));

            // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
            if (!cbSplit._compliantExecNpcg && match.length > 1) {
                match[0].replace(separator2, function () {
                    for (var i = 1; i < arguments.length - 2; i++) {
                        if (arguments[i] === undefined) {
                            match[i] = undefined;
                        }
                    }
                });
            }

            if (match.length > 1 && match.index < str.length) {
                Array.prototype.push.apply(output, match.slice(1));
            }

            lastLength = match[0].length;
            lastLastIndex = lastIndex;

            if (output.length >= limit) {
                break;
            }
        }

        if (separator.lastIndex === match.index) {
            separator.lastIndex++; // avoid an infinite loop
        }
    }

    if (lastLastIndex === str.length) {
        if (lastLength || !separator.test("")) {
            output.push("");
        }
    } else {
        output.push(str.slice(lastLastIndex));
    }

    return output.length > limit ? output.slice(0, limit) : output;
};

cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined; // NPCG: nonparticipating capturing group
cbSplit._nativeSplit = String.prototype.split;

} // end `if (!cbSplit)`

// for convenience...
String.prototype.split = function (separator, limit) {
    return cbSplit(this, separator, limit);
};

在试用上面的代码并删除缓存后,发现它什么也没做......任何人都可以提前帮助,亲切的问候。

感谢EdoDodo上面的代码,但是你可以提供任何进一步的帮助,因为我几乎撕掉我的头发,它最终没有工作,有一点需要注意,主页上的链接按钮(如果注释掉了)使该网站适用于主页,错误消失但我真的希望主页上每个帖子摘录的链接按钮。

网站是:

www.mobileinquirer.com

1 个答案:

答案 0 :(得分:2)

Firefox在脚本的这一部分第913行显示了一个脚本错误:

<script type="text/javascript">
    // <![CDATA[
        var disqus_shortname = 'mobileinquirer';
        var disqus_domain = 'disqus.com';
        (function () {
            var nodes = document.getElementsByTagName('span');
            for (var i = 0, url; i < nodes.length; i++) {
                if (nodes[i].className.indexOf('dsq-postid') != -1) {
                    nodes[i].parentNode.setAttribute('data-disqus-identifier', nodes[i].getAttribute('rel'));
                    url = nodes[i].parentNode.href.split('#', 1);
                    if (url.length == 1) url = url[0];
                    else url = url[1]
                    nodes[i].parentNode.href = url + '#disqus_thread';
                }
            }
            var s = document.createElement('script'); s.async = true;
            s.type = 'text/javascript';
            s.src = 'http://' + disqus_domain + '/forums/' + disqus_shortname + '/count.js';
            (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
        }());
    //]]>
    </script>

具体错误在这一行:

url = nodes[i].parentNode.href.split('#', 1);

这是因为parentNode没有href。此错误与拆分功能无关。代码尝试获取parentNode上的href属性的值,但是没有href属性,因此解析为undefined,因此对split的调用失败。它与split函数无关。问题是你的标记显然是错误的,我认为disqus代码期望标签周围有一个标签,但它找不到。

如果你看一下mobilinquirer.com HTML源代码中的664-665行,你会在该行找到这个序列,然后几次:

<p><span
class="dsq-postid">8 Comments</span></p>

此代码导致错误。 <span class="dsq-postid">代码必须包含<a href="xxx">代码作为其父代,否则您将收到此错误。我在HTML中看到了同样的问题。

此问题与拆分功能无关。为了消除此错误,您需要修复HTML以使其成为disqus代码所期望的内容或删除有问题的disqus代码(您似乎不需要)或两者兼而有之。