Results 1 to 6 of 6

Thread: [JS] If Statement with "or" (||) - can you see a problem?

  1. #1
    126
    posts
    Registered User

    [JS] If Statement with "or" (||) - can you see a problem?

    I have an if statement, as seen below.
    Code:
    if (l[i].href.split('/')[2].replace(/www\./, '') != document.domain.replace(/www\./, '')
          && !l[i].getAttribute('target')
          && !l[i].parentNode.id.match(/^copy/)
          && l[i].href.indexOf(your_domain) == -1) {
          //etc, etc
    }
    This works ok. But as soon as I change the last "and" (&&) to an "or" (||), there is a problem.
    Code:
    if (l[i].href.split('/')[2].replace(/www\./, '') != document.domain.replace(/www\./, '')
          && !l[i].getAttribute('target')
          && !l[i].parentNode.id.match(/^copy/)
          || l[i].href.indexOf(your_domain) == -1) {
          //etc, etc
    }
    This if statement is encased in a for loop, and with the "or" there, the loop just keeps on going, as if there is something not closed properly.
    Can anyone see a problem with the second piece of code above and why it doesn't work when the first one does? Is there something else I can add in to get this working as it should?
    Thanks in advance

  2. #2
    Add parentheses around the "and" items. The boolean is seeing if the first two are true, and either of the last two are, I believe.

    Parentheses should fix it.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  3. #3
    HTML Code:
    if ((l[i].href.split('/')[2].replace(/www\./, '') != document.domain.replace(/www\./, '')
          && !l[i].getAttribute('target')
          && !l[i].parentNode.id.match(/^copy/))
          || (l[i].href.indexOf(your_domain) == -1)) {
          //etc, etc
    }
    May fix it

  4. #4
    Which is exactly what I said.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  5. #5
    Haha, ok

  6. #6
    Well, you gave source, so some credit goes to you.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012