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://www.chineselearnonline.com/amember/member.php',
'http://www.chineselearnonline.com/amember/profile.php'
]
if (currentPage.indexOf(2) == -1 ) {
document.getElementsByClassName('grey-block')[0]
.insertAdjacentHTML('afterend', '<div style="top:124px;" class="orange-block-2"><a href="http://clo2015.chineselearnonline.com/">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://clo2015.chineselearnonline.com/">Try out the New Version of CLO</a></div>')
}
}
But as you can see: http://www.chineselearnonline.com/nlevel1 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
currentPage.indexOf(2)it will check the entry as "2" in array which does not exist mean it will always return -1 and which it will true for your condition and it will execute if condition only.. it will never fall in else condition