#1
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19

    Javascript "or" statement


    Hi,
    I'm trying to get this script to work

    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000">
    <script>
    var url1 = 'http://www.degs.co.uk/test/javascript/location.htm';
    var url2 = 'http://www.degs.co.uk/test/javascript/location2.htm';
    var welcome = 'Welcome to Index Page';
    var goHome = 'Go Back to Index Page';
    
    if (location.href == (url1 || url2))
    {
    document.write(welcome);
    }
    else {
    document.write(goHome);
    }
    </script>
    </body>
    </html>
    if (location.href == (url1 || url2))
    Is that the correct way to have two statements/an "or" statement.

    At the moment it isn't working.
    They all display the goHome variable.

    If I use
    if (location.href == ((url1) || (url2)))
    They all display the goHome variable.


    If I use
    if (location.href == (url1) || (url2))
    The all display the welcome variable.




    http://www.degs.co.uk/test/javascript/location.htm
    http://www.degs.co.uk/test/javascript/location2.htm
    http://www.degs.co.uk/test/javascript/location3.htm
    Last edited by degsy; January 24th, 2002 at 10:27 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    var url1 = 'http://www.degs.co.uk/location.htm';
    var url2 = 'http://www.degs.co.uk/location2.htm';
    var msg = (location.href.indexOf(url1)>=0 || location.href.indexOf(url2)>=0) ?
    'Welcome to Index Page' : 'Go Back to Index Page';
    document.write(msg);


    http://www.xs4all.nl/~ppk/js/boolean.html
    Last edited by mrrichardfeder; January 24th, 2002 at 10:30 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    Works perfectly
    Thanks.

    Could you please explain what
    var msg = (location.href.indexOf(url1)>=0 || location.href.indexOf(url2)>=0) ?
    'Welcome to Index Page' : 'Go Back to Index Page';

    does please.
    Especially the ? and the :

    Still learning this JS stuff.

    Cheers,
    Degs
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    Thanks for the link. It explains things nice & simply
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    Cheers, degsy...

    OK: the JavaScript conditional (also ternary or 'three-part') operator provides a shorthand syntax for choosing between two (or more) alternatives. First, a conditional expression:

    (location.href.indexOf(url1)>=0 || location.href.indexOf(url2)>=0) ?

    ...followed by a question mark; the parentheses are optional, but make the structure clearer. This translates to: if (condition is true). This is where this operator differs from a standard if..else statement - it contains the if and the else right in the same statement:

    do this : (else)do this;

    If the conditional expression - these are expressions, not statements - evaluates to true, the operand before the ':' is assigned; if false, the operand after the ':' is assigned. Just like if...else, only more compact. In point of fact, the JS runtime engine has to perform the necessary logical calculations equally for both operators, so the conditional one is no more efficient; it just looks less cluttered (cooler too). Where it really comes into its own is when you cascade them:

    function getElement(id) {
    return document.all ? document.all(id) :
    document.getElementById ? document.getElementById(id) :
    document.layers ? document.layers[id] :
    null;
    }

    This is a bit like the JS 'switch/case' statement - if the first conditional (is there a document.all object - as in 'IE') is true, then an element reference using document.all is returned; if not, the program goes to the RH operand - but it has a '?' as well, so, it becomes the next conditional, and the choosing process continues....the 'null' value is the default. Notice how the return statement operates on whatever value the logical expressions come back with. You can run code (JS statements) this way, do things other than just assigning something to a variable. Very useful.

    In the above function, the ternary operator simply determines which string is assigned to the msg variable. I used the indexOf() method - instead of trying to match the href exactly - to avoid the possibility of a query string (or, more likely, a top-of-page '#') from fouling up the comparison. Just a quick guess.

    http://hotwired.lycos.com/webmonkey/...x0a_page3.html

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    Thank you for the explaination.
    Much appreciated

    Cheers,
    Degs

IMN logo majestic logo threadwatch logo seochat tools logo