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 01-13-2013, 02:44 PM   PM User | #1
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
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>
chickentulip is offline   Reply With Quote
Old 01-13-2013, 03:19 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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>
DaveyErwin is offline   Reply With Quote
Old 01-13-2013, 03:33 PM   PM User | #3
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
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>
chickentulip is offline   Reply With Quote
Old 01-13-2013, 03:56 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
else (window.ActiveXObject) {
is a syntax error
should be

else{
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
chickentulip (01-13-2013)
Old 01-13-2013, 04:58 PM   PM User | #5
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
i cant believe i did't notice that myself..
thank you very much
chickentulip is offline   Reply With Quote
Old 01-13-2013, 08:22 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 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
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
chickentulip (01-14-2013)
Old 01-13-2013, 08:47 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-13-2013, 10:10 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 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
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.
Old Pedant is offline   Reply With Quote
Old 01-13-2013, 10:18 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 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
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.
Old Pedant is offline   Reply With Quote
Old 01-13-2013, 11:39 PM   PM User | #10
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 892
Thanks: 4
Thanked 206 Times in 205 Posts
tracknut is an unknown quantity at this point
Your site doesn't happen to be www.fansofoutdatedversionsofie.com, does it?



Dave
tracknut is offline   Reply With Quote
Old 01-14-2013, 04:35 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 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
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.
Old Pedant is offline   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 01:56 AM.


Advertisement
Log in to turn off these ads.