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 resultMy 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) {
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;
}) {
} else {
}
}
. !
# 2 29
- , . , .

№ 3 29
. , , , ,

, 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) {
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');
}
}
}