繁体   English   中英

拆分问题Internet Explorer

[英]Split Problem Internet Explorer

与该错误有关的更早的问题之后。

错误:无法获取属性“ split”的值:对象为null或未定义

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

/* 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

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。 此错误与split函数无关。 该代码试图获取父节点上href属性的值,但是没有href属性,因此解析为undefined,因此拆分调用失败。 它与split功能无关。 问题是您的标记显然是错误的,我认为Disqus代码期望标记周围有标记,但找不到。

如果您在mobilinquirer.com HTML源代码中查看第664-665行,则会在该行找到此序列,然后在以下几处进行查找:

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

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

此问题与split函数无关。 要使此错误消失,您需要修复HTML,以便它是Disqus代码所期望的,或者删除有问题的Disqus代码(您似乎不需要)或同时删除两者。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM