Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try question 1

Status
Not open for further replies.

seanybravo

IS-IT--Management
Sep 24, 2003
89
GB
I have come across this piece of code:

try {

// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");

}
catch (e) {
// browser doesn't support ajax. handle however you want
}

is the : a shortcut to try differient bits of code because I would like to put in another line in. It looks like it tries two lines of code to me. I have tested it and it works but falls over when I add an extra line:

try {
objXmlHttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP"):
new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
alert ("Cant start XMLHTTPRequest!")
}

Any feedback would be great.
 
Code:
a=b?c:d
[code]
means
[code]
if b then a=c else a=d
So it makes no sense asigning two values.

Cheers,
Dian



 
Your question isn't actually a question about they try/catch block, it's a question about the ternary operator

If you view the link above you'll see that the ternary operator evaluates a condition which is followed by a ?

In your case it is:
Code:
window.XMLHttpRequest?

Generally I will encapsulate the condition in parentheses to make it easier to read. If what is evaluated in this condition is true, it will return what is after the question mark, if it evaluates what is false it will return what is after the colon, like so:
Code:
(some condition) ? value if true : value if false

So, you cannot just add on another : to give another return value - the condition can either be true or false, so having 3 return values does not make sense.

However, you can chain ternary operators together. Here's an example:
Code:
var a = 1
var b = 2

var c = (a == 1) ? [!](b == 3) ? 4 : 5[/!] : 6;

The portion I have highlighted in red is a nested ternary operator and only gets evaluated if a is 1. In the example I gave above, c would equal 5.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Multiple try-catch progression would be this.
[tt]
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest;
} catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
// browser doesn't support ajax. handle however you want
}
}
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top