Not sure why indexOf() method is not working with my conditional in JavaScript

I have a function which is going through links in a document to determine if a certain pathname is present. Essentially if it is present target _self is set. If not I'd like to have the target attribute set to _blank

/*   These are properties of a larger function,   I am just trying to zero in on the problems.  */        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]];          }      }    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');          }                 }  }

What is bedeviling me is this:

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

Shouldn't that be the key? if that url is not equal to -1 that means it's a match so link.setAttribute('target', '_self'); should be set to the a tag. And if it is equal to -1 link.setAttribute('target', '_blank');

Replay

Category: javascript Time: 2016-07-28 Views: 0
Tags: javascript

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.110 (s). 12 q(s)