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

6
  • 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 Commented Apr 6, 2015 at 3:35
  • @ArunPJohny Yes, I'll word it better. Commented Apr 6, 2015 at 3: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. Commented Apr 6, 2015 at 3:40
  • @TechMa9iac So adding a third one will solve the problem? Commented Apr 6, 2015 at 3:40
  • 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 Commented Apr 6, 2015 at 3:42

2 Answers 2

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>')
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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>')
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.