lundi 27 juin 2016

Can't seem to get the break statement working correctly in conditional and for-loop in JavaScript

I am looping through a collection of links in the DOM and trying to set an attribute based on a condition. While doing so i'd like to strip out a specific string after the base URL:

http://smodev.rye.avon.com/XXXX/page.aspx

so I'd get:

page.aspx

So far I managed to get the base URL off, and at first I thought to use a regex, to zero in on /XXXX/, but I'd like to use this script in another context, because at other instances having just the pathname is swell:

/XXXX/XXXX/page.aspx

Naturally I thought using a conditional with the includes and replace methods in the following would work.

  var MarkUpChecker = (function iffe() {
    var publicAPI = {
        getURL: function() {
            for (var i = 0; i < arguments.length; i++) {
                return {
                    'smo': 'http://smo.rye.avon.com',
                    'smodev': 'http://smodev.rye.avon.com',
                    'youravon_sans_w': 'http://youravon.com',
                    'youravon': 'http://www.youravon.com',
                    'youravon2': 'http://www2.youravon.com'
                }[arguments[i]];
            }
        },
        searchURL: function() {
            var link, url, parser, newPathName = '';
            for (var i = 0, len = arguments.length; i < len; i++) {
                url = this.getURL(arguments[i]);
                for (var j = 0, jlen = document.links.length; j < jlen; j++) {
                    link = document.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.includes('/Executive/')) {
                            var newstr = newPathName.replace('/Executive/', '');
                            break;
                        }
                        if (newPathName.includes('/District/')) {
                            newstr = newPathName.replace('/District/', '');
                            break;
                        }
                        if (newPathName.includes('/Division/')) {
                            newstr = newPathName.replace('/Division/', '');
                            break;
                        }
                        if (newPathName.includes('/National/')) {
                            newstr = newPathName.replace('/National/', '');
                            break;
                        }
                        link.href = newstr;

                    }

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

When stepping through the debugger I noticed in the first pass it works! �/ However as the loop progresses, It becomes a mess as you can see here:

enter image description here

Shouldn't the newstr variable just return:

/XXXX/XXXX/XXXX/page.aspx

Otherwise just return

page.aspx

when it encounters '/Executive/', '/District/', '/Division/', '/National/'

Aucun commentaire:

Enregistrer un commentaire