0

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

Community
  • 1
  • 1
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • sorry.. what do you mean... do you mean if the url of the current page matches an entry in the array then you want the if block to executed – Arun P Johny Apr 06 '15 at 03:35
  • @ArunPJohny Yes, I'll word it better. – alexchenco Apr 06 '15 at 03:36
  • Since the currentPage array has got only two elements, currentPage.indexOf(2) == -1 will evaluate to true and hence the first if block will be executed. – Manish Kr. Shukla Apr 06 '15 at 03:40
  • @TechMa9iac So adding a third one will solve the problem? – alexchenco Apr 06 '15 at 03:40
  • Move the first if statement in the end, that's condition should be executed at last if nothing matches up. So you have all your specific condition checks in the beginning and at last the generic one. – Manish Kr. Shukla Apr 06 '15 at 03:42
  • 1
    `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 – Deepak Sharma Apr 06 '15 at 03:42

2 Answers2

2

In your code you are always checking the index of the numeric value 2 in the array, since that value is not present it will always return -1 so the if block is always executed.

Since you want to check the url of the current page, you can try to see whether document.URL is present in the array.

window.onload = function () {
    var currentPage = [
        'http://www.chineselearnonline.com/amember/member.php',
        'http://www.chineselearnonline.com/amember/profile.php']

    if (currentPage.indexOf(document.URL) > -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>')
    }
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

Use window.location.href to check if your current page matches anything in the array:

window.onload = function() { 
    var currentPage = [
        'http://www.chineselearnonline.com/amember/member.php',
       'http://www.chineselearnonline.com/amember/profile.php'
    ]

    if (currentPage.indexOf(window.location.href) == -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>')
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360