Antonio Ortiz Antonio Ortiz - 1 year ago 200
Javascript Question

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:

so I'd get:


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


Naturally I thought using a
with the
methods in the following would work.

var MarkUpChecker = (function iffe() {
var publicAPI = {
getURL: function() {
for (var i = 0; i < arguments.length; i++) {
return {
'smo': '',
'smodev': '',
'youravon_sans_w': '',
'youravon': '',
'youravon2': ''
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/', '');
if (newPathName.includes('/District/')) {
newstr = newPathName.replace('/District/', '');
if (newPathName.includes('/Division/')) {
newstr = newPathName.replace('/Division/', '');
if (newPathName.includes('/National/')) {
newstr = newPathName.replace('/National/', '');
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
variable just return:


Otherwise just return


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

Answer Source

break is not doing what you expect -- it is not like in a case statement -- it makes you exit the inner for loop!

Secondly, if none of the if conditions is true, you assign a newstr value that is either undefined, or the value set during a previous iteration. That really causes a tremendous mix-up.

Instead use else:

if (newPathName.includes('/Executive/')) {
    var newstr = newPathName.replace('/Executive/', '');
} else if (newPathName.includes('/District/')) {
    newstr = newPathName.replace('/District/', '');
} else if (newPathName.includes('/Division/')) {
    newstr = newPathName.replace('/Division/', '');
} else if (newPathName.includes('/National/')) {
    newstr = newPathName.replace('/National/', '');

But looking at what you try to do here, you can do that in one replace:

var newstr = newPathName.replace('/Executive|District|Division|National/', '');

If your goal is to remove the path completely when one of these words occurs, then do this:

if(|District|Division|National/) != -1) {
    newPathName = newPathName.split('/').pop();
linkhref = newPathName;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download