Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Putting an "or" in javascript

    Hi,
    I have some code that I want to include an "or" option into the code but not having much luck with it because I'm not too javascript savvy

    Code:
    if(anchorSrc.match("mysite.co.uk")&&anchorVal.match("Great Site"))
    Basically the above code makes sure that a link to mysite.co.uk is in place and that it uses the link text "Great Site".
    What I want to do is add that the "anchorVal.match" can be "Great Site" OR "Super Site" if that makes sense but I'm not having much luck with it.

    Any help would be appreciated, thanks!
    Last edited by Wardy7; 02-21-2013 at 02:35 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    "and" in Javascript is &&
    "or" is ||

    You must use brackets to group the conditions:-

    Code:
    if ( (anchorSrc.match("mysite.co.uk")) && ((anchorVal.match("Great Site")) || (anchorVal.match("Super Site"))) ) {

    Customer in UK travel agent shop: - "What month is it now in Australia?"

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Wardy7 (02-21-2013)

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    37
    Thanks
    1
    Thanked 4 Times in 4 Posts
    "||" is be the statement for "or" in JavaScript as earlier stated by Phillip M.

    The link below is a good reference to these Logical Operators provided from W3Schools.com -- I personally use this site as a coding reference.

    http://www.w3schools.com/js/js_comparisons.asp

  • #4
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    "and" in Javascript is &&
    "or" is ||

    You must use brackets to group the conditions:-

    Code:
    if ( (anchorSrc.match("mysite.co.uk")) && ((anchorVal.match("Great Site")) || (anchorVal.match("Super Site"))) ) {

    Customer in UK travel agent shop: - "What month is it now in Australia?"
    Philip you are an absolute star man that is brilliant and does the trick! Thanks also for the explanation of the || for an "or" too, it would explain why my code didn't like it when I tried adding in an "or" into it.

    One other slight deviation to this question to save me starting a new thread, but is there any simple way to make the "anchorVal.match" parts not to be case sensetive like they currently are or am I best just adding lots of "or" statements in to cover a few variations that users might do if they change the case?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    if ((anchorSrc.toLowerCase().match("mysite.co.uk")) && ((anchorVal.toLowerCase().match("great site")) || (anchorVal.toLowerCase().match("super site"))) ) {

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Obsidian View Post
    "||" is be the statement for "or" in JavaScript as earlier stated by Phillip M.

    The link below is a good reference to these Logical Operators provided from W3Schools.com -- I personally use this site as a coding reference.

    http://www.w3schools.com/js/js_comparisons.asp
    That particular page is fine, but sadly a good deal of w3schools.com which was great 10-12 years ago is very much out of date, and is not a very good exemplar of modern Javascript.
    Alerts, prompts, document.write(), and quite a few other things covered in the tutorials are all obsolete!

    The best and most up-to-date tutorials I know are at http://www.javascriptexample.net/
    Last edited by Philip M; 02-21-2013 at 03:47 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Another way to do it:
    Code:
    if (    anchorSrc.match(/mysite.co.uk/i) 
         && ( anchorVal.match(/great site/i) || anchorVal.match(/super site/i) )
    ) {
    The /i there means "ignore case". Technically, the argument to match SHOULD be a regular expression. When you use a string instead, as Philip did, it is converted to a regular expression for you, but then you can't specify /i or /g.

    Another way, even better:
    Code:
    if (    anchorSrc.match(/mysite.co.uk/i) 
         && anchorVal.match(/(great site|super site)/i) 
    ) {
    A regular expression can be used to match any number of things if they are separated by | and grouped in ( ).

    Still another way (completely equivalent for these purposes):
    Code:
    if ( (/mysite.co.uk/i).test(anchorSrc) && (/(great site|super site)/i).test(anchorSrc) ) {
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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