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 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts

    Why "else if", not "else"?

    I was wondering if someone could take a look at this short script, and tell me why it not working with just "else" but works with "else if"? I assumed, (i guess, mistakenly), that in some instances, there is no difference between else and else if.

    Thank you very much for your help.



    Code:
    <Doctype html>
    <html>
    <head>
    <title>
    AJAX
    </title>
    </head>
    <body>
    <script>
    var httpRequest;
    if (window.XMLHttpRequest) { 
        httpRequest = new XMLHttpRequest();
    	alert("HELLO");
    } 
    else if(window.ActiveXObject) { 
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    </script>
    </html>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by chickentulip View Post
    I was wondering if someone could take a look at this short script, and tell me why it not working with just "else" but works with "else if"? I assumed, (i guess, mistakenly), that in some instances, there is no difference between else and else if.

    Thank you very much for your help.



    Code:
    <Doctype html>
    <html>
    <head>
    <title>
    AJAX
    </title>
    </head>
    <body>
    <script>
    var httpRequest;
    if (window.XMLHttpRequest) { 
        httpRequest = new XMLHttpRequest();
        alert("HELLO");
    } 
    else if(window.ActiveXObject) { 
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
     
    </script>
    </html>
    </body>
    </html>
    Code:
     
    <Doctype html>
    <html>
    <head>
    <title>
    AJAX
    </title>
    </head>
    <body>
    <script>
    var httpRequest;
    if (window.XMLHttpRequest) { 
        httpRequest = new XMLHttpRequest();
     alert("HELLO");
    } else { 
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    alert(httpRequest)
    </script>
    </html>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts
    thank you very much for the code. if i replace else if with else in my code, the firefox error console tell me that there is a syntax error and shows me the line with else statement. I don't understand why it happens

    Code:
    <Doctype html>
    <html>
    <head>
    <title>
    AJAX
    </title>
    </head>
    <body>
    <script>
    var httpRequest;
    if (window.XMLHttpRequest) { 
        httpRequest = new XMLHttpRequest();
    	alert("HELLO");
    } 
    else (window.ActiveXObject) { 
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    	
    }
    
    </script>
    </html>
    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    else (window.ActiveXObject) {
    is a syntax error
    should be

    else{

  • Users who have thanked DaveyErwin for this post:

    chickentulip (01-13-2013)

  • #5
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts
    i cant believe i did't notice that myself..
    thank you very much

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,547
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    But *TWO* better/simpler ways to do this:
    Code:
    var httpRequest = (window.XMLHttpRequest != null) 
                    ? new XMLHttpRequest()
                    : new ActiveXObject("Microsoft.XMLHTTP");
    or
    Code:
    var httpRequest;
    try {
        httpRequest = new XMLHttpRequest();
    } catch ( ignored ) {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    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.

  • Users who have thanked Old Pedant for this post:

    chickentulip (01-14-2013)

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    As IE6 is now dead you could even reduce it down to just:

    Code:
    var httpRequest = new XMLHttpRequest();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,547
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    No, sorry Felgall, it's not dead.

    I see hits from at least 4 different users on one of our sites just yesterday, all using MSIE 6.0, believe it or not. One of them even using Windows 98!!

    Three of those IE6 users from France, one from Canada.

    (Granted, that is 4 users--18 page hits total--out of over 26,000 page hits. So less than 0.1%. But it's clearly not zero.)

    So for now, at least, I think I'll keep providing for IE6, unfortunately.
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,547
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Oh, wow! It's a *LOT* more than I thought! Teach me to use too small a sample!

    4533 unique IP addresses yesterday.

    79 (yes, really, 79!!) were using MSIE 6 !!!

    That's ALMOST 2% of our users!

    WHEW! Sorry, but I don't think I'm ready to abandon MSIE 6 support, yet!

    Just for grins, I also queried for MSIE 5. You ready for this: 10 IP addresses still using MSIE 5.5 and even ONE still using MSIE 5.0!!!! (the 5.0 user was another Win98 user).

    WOW!
    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.

  • #10
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,341
    Thanks
    11
    Thanked 288 Times in 287 Posts
    Your site doesn't happen to be www.fansofoutdatedversionsofie.com, does it?



    Dave

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,547
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Quote Originally Posted by tracknut View Post
    Your site doesn't happen to be www.fansofoutdatedversionsofie.com, does it?
    LOL!

    Not at all! In fact, a lot of the newer pages are really designed to take advantage of some fairly advanced features of CSS.

    I was blown away to see those MSIE 5 people still hitting the site! It scares me to wonder what some of those pages may look like, to them.

    I suspect I'll be talking to the page design guy tomorrow! (I do mostly the backend stuff and then the JS needed to support it all, but leaving most of the actual design decisions up to him.)
    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
    •