不确定为什么indexOf()方法不适用于JavaScript中的条件

时间:2016-07-28 22:15:08

标签: javascript

我有一个函数,它通过文档中的链接来确定是否存在某个路径名。基本上如果它存在target _self已设置。如果不是我希望将目标属性设置为_blank

/* 
These are properties of a larger function, 
I am just trying to zero in on the problems.
*/


/* 
These are properties of a larger function, 
I am just trying to zero in on the problems.
*/

var MarkUpChecker = (function iffe() {
    'use strict';
    var URLS = {
            'foo': 'http://foo.com',
            'bar': 'http://bar.com',
            'yay': 'http://www.yay.com'
        },

        publicAPI;

    function getURL() {
        for (var i = 0; i < arguments.length; i++) {
            return URLS[arguments[i]];
        }
    }
    // function setImgSrc(){

    // }
    publicAPI = {

        addURL: function() {
            for (var i = 0; i < arguments.length; i += 2) {
                URLS[arguments[i]] = arguments[i + 1];
            }
            console.dir(URLS);
            return URLS;
        },
        addTag: function() {
            var doc = document,
                internal_h1 = doc.querySelectorAll('.internal_h1'),
                sheet = doc.createElement('style');
            sheet.innerHTML = '.archive {font-family: Helvetica Neue, Helvetica, Arial, sans-serif !important;font-size: 14px !important;line-height: 20px !important;color: #333 !important;font-weight: 200 !important;padding: 0px 5px 0px 0px !important;} .archive a {color: #ec008c;display: inline-block;padding: 0 14px;} .file_folder {position: relative;top: 5px;padding: 0 5px;};'
            doc.body.appendChild(sheet);
            if (internal_h1) {
                for (var i = 0; i < internal_h1.length; i++) {
                    internal_h1[i].innerHTML = '<h1>' + internal_h1[i].innerHTML + '</h1>';
                    sheet.innerHTML = 'h1 {font-family: Helvetica, Arial, sans-serif !important; font-weight: 200!important; font-size: 22px !important; color: #333; margin: 3px 0px 6px; line-height: 24px !important;} .archive {font-family: Helvetica Neue, Helvetica, Arial, sans-serif !important;font-size: 14px !important;line-height: 20px !important;color: #333 !important;font-weight: 200 !important;padding: 0px 5px 0px 0px !important;} .archive a {color: #ec008c;display: inline-block;padding: 0 14px;} .file_folder {position: relative;top: 5px;padding: 0 5px;};'
                    doc.body.appendChild(sheet);
                }
            }
        },

        searchDoc: function() {
            var link, url, parser, newPathName = '',
                emailUrl = /\/img\//,
                newstr = '',
                doc = document,
                container,
                container_links,
                container_images,
                documentTableWrapper,
                docBodyFirstChild;

            if (!doc.getElementById('container')) {
                container = doc.createElement('div');
                container.setAttribute('id', 'avon_rep_container');

                container_links = container.getElementsByTagName('a');
                documentTableWrapper = doc.getElementsByTagName('table')[0];
                container.appendChild(documentTableWrapper);
                container.className = 'news';
                docBodyFirstChild = doc.body.firstChild;
                doc.body.insertBefore(container, docBodyFirstChild);

            } else {
                container_links = doc.getElementById('container').getElementsByTagName('a');
            }
            container_images = container.getElementsByTagName('img');
            for (var i = 0; i < avon_rep_container_images.length; i++) {
                if (arguments[0] == "foo") {
                    container_images[i].src = container_images[i].src.replace(emailUrl, '/images/news/');
                } else {
                    container_images[i].src = container_images[i].src.replace(emailUrl, '/static/images/alt_news/');

                }
            }

            for (var i = 0, len = arguments.length; i < len; i++) {
                url = getURL(arguments[i]);
                for (var j = 0, jlen = container_links.length; j < jlen; j++) {
                    link = container_links[j];
                    if (link.href.indexOf(url) !== -1) {
                        parser = document.createElement('a');
                        parser.href = link.href;

                        newPathName = parser.pathname;
                        link.setAttribute('target', '_self');

                        if (newPathName.search(/Executive|District|Division|National/) !== -1) {
                            newPathName = newPathName.split('/').pop();
                            newstr = newPathName;

                        } else {
                            newstr = newPathName;

                        }
                        link.href = newstr;

                    } else {

                        link.setAttribute('target', '_blank');
                    }
                  
                }

            }
        }
    };
    return publicAPI;
})();   

困扰我的是:

if (link.href.indexOf(url) !== -1) {

这不应该是关键吗?如果该网址不等于-1,则表示匹配,因此link.setAttribute('target', '_self');应设置为a tag。 如果它等于-1 link.setAttribute('target', '_blank');

先谢谢啦!

更新 我按要求添加了完整的脚本!

2 个答案:

答案 0 :(得分:1)

您的循环结构错误。您正在为每个URL循环容器中的所有链接,这将为最后一个URL的每个链接生成有效结果。相反,你想要像

这样的东西
var urls = [];
for (var i = 0, len = arguments.length; i < len; i++) {
    urls[i] = getURL(arguments[i]);
}
for (var j = 0, jlen = container_links.length; j < jlen; j++) {
    link = container_links[j];
    if (urls.every(function(url) {
        return link.href.indexOf(url) !== -1;
    }) {
        // none of the urls matched the link
    } else {
        // at least one of the urls matched the link
    }
}

答案 1 :(得分:0)

函数getURL不完整。缺少添加确定将定义返回的键的参数:

function getURL(arg) {
    return URLS[arg];
}

如果URL包含json键,则返回链接,否则返回undefined。

之后,您可以继续逻辑。