dimanche 5 avril 2015

If statement that loops through array not working

If the URL of the current page matches the entry array the code inside the upper part of the if statement should execute, otherwise the code inside the bottom part (else) will execute:



window.onload = function() {
var currentPage = [
'http://ift.tt/1oLA1dd',
'http://ift.tt/1HFo4wt'
]

if (currentPage.indexOf(2) == -1 ) {
document.getElementsByClassName('grey-block')[0]
.insertAdjacentHTML('afterend', '<div style="top:124px;" class="orange-block-2"><a href="http://ift.tt/1xGghOG">Try out the New Version of CLO</a></div>')
} else {
document.getElementsByClassName('grey-block')[0]
.insertAdjacentHTML('afterend', '<div class="orange-block-2"><a href="http://ift.tt/1xGghOG">Try out the New Version of CLO</a></div>')
}
}


But as you can see: http://ift.tt/1GBFefE the code inside the upper if runs anyway (the div with the top:124px shows up).


What am I doing wrong?


I took the code from this question: javascript If statement, looking through an array


Aucun commentaire:

Enregistrer un commentaire