Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2013, 02:30 PM   PM User | #1
Wardy7
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Wardy7 is an unknown quantity at this point
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..
Wardy7 is offline   Reply With Quote
Old 02-21-2013, 03:04 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
"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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Wardy7 (02-21-2013)
Old 02-21-2013, 03:11 PM   PM User | #3
Obsidian
New Coder

 
Join Date: Feb 2013
Posts: 28
Thanks: 1
Thanked 4 Times in 4 Posts
Obsidian is an unknown quantity at this point
"||" 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
Obsidian is offline   Reply With Quote
Old 02-21-2013, 03:21 PM   PM User | #4
Wardy7
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Wardy7 is an unknown quantity at this point
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?
Wardy7 is offline   Reply With Quote
Old 02-21-2013, 03:34 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 02-21-2013, 03:41 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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/
__________________

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.

Last edited by Philip M; 02-21-2013 at 03:47 PM..
Philip M is offline   Reply With Quote
Old 02-21-2013, 11:42 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,565
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:31 PM.


Advertisement
Log in to turn off these ads.