html - Can't figure out what's wrong with my if statement in JavaScript [Update Sept 1st It appears my for loop is running twice, how do I fix?] -
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() { (var = 0; < arguments.length; i++) { return urls[arguments[i]]; } } publicapi = { addurl: function() { (var = 0; < arguments.length; += 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'); (var = 0; < 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'); (var = 0; < 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/'); } } (var = 0, len = arguments.length; < len; i++) { url = geturl(arguments[i]); (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; })();
my problem conditional doesn't seem add target _self
attribute href
based on condition think should.
this excerpt of function can found in snippet above.
(var = 0, len = arguments.length; < len; i++) { url = geturl(arguments[i]); (var j = 0, jlen = container_links.length; j < jlen; j++) { link = container_links[j]; if (link.href.indexof(url) != -1) { //problem seems 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 background script's main function automate redundant tasks have weekly newsletter repurposed 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 dom, looks urls match condition , strips out base url, of pathname , applies target _self
href
it changes pathnames of images!
in html call function accepts string:
markupchecker.searchurl('foo', 'bar');
that string represents url target , have changes applied :
var urls = { 'foo': 'http://foo.com', 'bar': 'http://bar.com', 'baz': 'http://baz.com', 'yay': 'http://www.yay.com', 'blargh': 'http://www.blargh.com' },
this function used cycle through object:
function geturl() { (var = 0; < arguments.length; i++) { return urls[arguments[i]]; } }
and lastly searchurl
function:
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'); (var = 0; < 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/'); } } (var = 0, len = arguments.length; < len; i++) { url = geturl(arguments[i]); (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'); } } } }
what have observed via debugger while does strip out path name applies target _blank
hrefs
also—the funny thing script works in context/environment doesn't need go through part of code:
if (newpathname.search(/executive|district|division|national/) != -1) { newpathname = newpathname.split('/').pop(); newstr = newpathname; link.href = newstr; }
it works in case.
thank in advance!
update august 29th, 2016
someone mentioned try this:
var urls = []; (var = 0, len = arguments.length; < len; i++) { urls[i] = geturl(arguments[i]); } (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 urls matched link } else { // @ least 1 of urls matched link } }
but can't integrate larger code. appreciated!
update #2 on august 29th
in post, mentioned 1 of function logic may wrong. unfortunately person didn't far in details.
update #3 on august 29th o.k. think close, stepped through code , added breakpoints , appears code looping through links twice
the screenshots show how target _self
being applied links they're supposed to, reason loop runs again , jumps else block, adding target _blank
links?
here code again original loop! thanks!
for (var = 0, len = arguments.length; < len; i++) { url = geturl(arguments[i]); (var j = 0, jlen = container_links.length; j < jlen; j++) { link = container_links[j]; if (link.href.indexof(url) != -1) { //problem seems 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'); } } }
there multiple issues in code, including logical. example
function geturl() { (var = 0; < arguments.length; i++) { return urls[arguments[i]];//always returns arguments[0] } }
i rewrote in such way takes , returns arrays (see jsfiddle below).
next issue
//root problem. loop see solution in jsfiffle (var = 0, len = arguments.length; < len; i++) { // using loop allow unlimited arguments passed in url = geturl(arguments[i]); // calling publicapi.geturl() method , passing looped argument above //and rewrite target again , again. remove outer loop (var j = 0, jlen = avon_rep_container_links.length; j < jlen; j++) { // loop goes on whole documents links... link = avon_rep_container_links[j]; if (link.href.indexof(url) !== -1) { //...and checking each argument passed in see if matches object value stored in geturl function e.g. switch statement.. parser = document.createelement('a'); //...if adding blank tag documents in document parser.href = link.href;//parser isn't used link.setattribute('target', '_self');//will changed in next loop newpathname = parser.pathname; if (newpathname.search(/executive|district|division|national/) != -1) { // added check these strings smo page newpathname = newpathname.split('/').pop(); newstr = newpathname; } else { newstr = newpathname; } link.href = newstr; } else { link.setattribute('target', '_blank'); } }
lots of variables declared not used. didn't touch them.
the issue target
fixed in updated jsfiddle.
Comments
Post a Comment