I can’t understand what happened to my if statement in JavaScript. [Update September 1st. My for loop appears to work twice, how can I fix it?]

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

        publicAPI;

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

    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');
            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;};'
                doc.body.appendChild(sheet);
            }
        },

        searchDoc: function() {
            function insertAfter(newNode, referenceNode) {
                referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
            }

            var link, url, parser, newPathName = '',
                emailUrl = /img\//gi,
                tsUrl = /\/REPSuite\/static\/html\/inews_archives\/img\//gi,
                newstr = '',
                doc = document,
                container,
                container_id,
                container_links,
                container_images,
                documentTableWrapper,
                docBodyFirstChild,
                nodeToTargetToInsertLP;

            if (!doc.getElementById('container')) {
                container = doc.createElement('div');
                container.setAttribute('id', 'container');
                container.className = 'container-avon-representative-news';
                container_links = container.getElementsByTagName('a');
                container_id = doc.getElementById('container');
                docBodyFirstChild = doc.body.firstChild;
                nodeToTargetToInsertLP = doc.getElementsByClassName('flexTile')[4];


                if (nodeToTargetToInsertLP) {
                    documentTableWrapper = doc.getElementsByClassName('marginfix')[0];
                    container.appendChild(documentTableWrapper);
                    insertAfter(container, nodeToTargetToInsertLP);
                } else {
                    documentTableWrapper = doc.getElementsByTagName('table')[0];
                    container.appendChild(documentTableWrapper);
                    doc.body.insertBefore(container, docBodyFirstChild);
                }


            } else {
                container_links = doc.getElementById('container').getElementsByTagName('a');
            }
            container_images = container.getElementsByTagName('img');
            for (var i = 0; i < container_images.length; i++) {
                if (arguments[0] == "foo" || arguments[1] == "bar") {
                    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;

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


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

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

            }
        }
    };
    return publicAPI;
})();
Run codeHide result

My problem is that my conditional code does not add the target attribute _selfto hrefbased on the state, which I think should.

This is an excerpt from a function that can be found in the snippet above.

        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) { //problem seems to be here
                    parser = document.createElement('a');
                    parser.href = link.href;

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


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

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

        }

To give you some information, the main function of the script is to automate the redundant tasks that I have to do weekly into a newsletter that redirects to a static web page / landing page:

HTML:

<a href="http://foo.com/path/to_page.aspx"> 
    <img src="img/some_image.jpg">
</a>

the script goes through the DOM, looks for URLs that match the condition, and removes the base URL, some path and applies the target _selftohref

!

HTML , :

    MarkUpChecker.searchURL('foo', 'bar');

URL-, , :

    var URLS = {
        'foo': 'http://foo.com',
        'bar': 'http://bar.com',
        'baz': 'http://baz.com',
        'yay': 'http://www.yay.com',
        'blargh': 'http://www.blargh.com'
    },

:

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

, , searchURL:

 searchDoc: function() {
            function insertAfter(newNode, referenceNode) {
                referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
            }

            var link, url, parser, newPathName = '',
                emailUrl = /img\//gi,
                tsUrl = /\/REPSuite\/static\/html\/inews_archives\/img\//gi,
                newstr = '',
                doc = document,
                container,
                container_id,
                container_links,
                container_images,
                documentTableWrapper,
                docBodyFirstChild,
                nodeToTargetToInsertLP;

            if (!doc.getElementById('container')) {
                container = doc.createElement('div');
                container.setAttribute('id', 'container');
                container.className = 'container-avon-representative-news';
                container_links = container.getElementsByTagName('a');
                container_id = doc.getElementById('container');
                docBodyFirstChild = doc.body.firstChild;
                nodeToTargetToInsertLP = doc.getElementsByClassName('flexTile')[4];


                if (nodeToTargetToInsertLP) {
                    documentTableWrapper = doc.getElementsByClassName('marginfix')[0];
                    container.appendChild(documentTableWrapper);
                    insertAfter(container, nodeToTargetToInsertLP);
                } else {
                    documentTableWrapper = doc.getElementsByTagName('table')[0];
                    container.appendChild(documentTableWrapper);
                    doc.body.insertBefore(container, docBodyFirstChild);
                }


            } else {
                container_links = doc.getElementById('container').getElementsByTagName('a');
            }
            container_images = container.getElementsByTagName('img');
            for (var i = 0; i < container_images.length; i++) {
                if (arguments[0] == "foo" || arguments[1] == "bar") {
                    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;

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


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

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

            }
        }

, , , , _blank hrefs

. , script /, :

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

}

.

!

29 2016

- , :

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

. !

# 2 29

- , . , .

enter image description here

№ 3 29 . , , , ,

enter image description here enter image description here

, target _self , , - else, target _blank ?

! !

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) { //problem seems to be here
            parser = document.createElement('a');
            parser.href = link.href;

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


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

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

}
+4
2

, .

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

, (. jsfiddle ).

  //root problem. this for loop See solution in JSFiffle
  for (var i = 0, len = arguments.length; i < len; i++) { // Using a for loop to allow unlimited arguments to be passed in
    url = getURL(arguments[i]); // We are calling the publicApi.getURL() method and passing the looped argument from above

    //and rewrite target again and again. I remove outer loop
    for (var j = 0, jlen = avon_rep_container_links.length; j < jlen; j++) { // This loop goes over the whole documents links...
      link = avon_rep_container_links[j];
      if (link.href.indexOf(url) !== -1) { //...and we are checking each argument passed in to see if it matches the object value stored in the getURL function e.g. like a switch statement..
        parser = document.createElement('a'); //...if so we are essentially adding a blank tag to all the documents in the document
        parser.href = link.href;//parser isn't used 

        link.setAttribute('target', '_self');//will be changed in the next loop
        newPathName = parser.pathname;


        if (newPathName.search(/Executive|District|Division|National/) != -1) { // Added check for these strings for SMO page
          newPathName = newPathName.split('/').pop();
          newstr = newPathName;

        } else {
          newstr = newPathName;
        }
        link.href = newstr;
      } else {
        link.setAttribute('target', '_blank');

      }
    }

, , . .

target ​​ JSFiddle.

+1

Edited!

id <a> jQuery, <a> target:

<a id="custom_target" href="http://foo.com/path/to_page.aspx"> 
    <img src="img/some_image.jpg">
</a>

jQuery :

if (link.href.indexOf(url) !== -1) {
    $('#custom_target').attr('target', '_self');
}
else{
    $('#custom_target').attr('target', '_blank');
}
0

Source: https://habr.com/ru/post/1651077/


All Articles