Reputation: 23
I'm a complete beginner to JavaScript and have been looking into this for quite some time now, I feel I've tried a lot of different ways of writing this code and it just doesn't seem to work as intended, nothing happens at all.
I want to convert this if statement:
if (browserName == "MSIE") {
} else if (browserName == "IE11+") {
} else if (browserName == "Chrome") {
} else if (browserName == "Firefox") {
} else if (browserName == "Safari") {
} else alert("your browser is not compatible, please use one of these web browsers: Google Chrome, Mozilla Firefox, Internet Explorer or Apple Safari.")
into a switch statement, this is what I currently have:
switch (browserName) {
case '1': == "MSIE"();
break;
case '2': == "Safari"();
break;
case '3': == "Chrome"();
break;
case '4': == "IE11+"();
break;
case '5': == "Firefox"();
break;
default:
alert("test1");
}
I just want it to skip to the else alert if it does not detect the browser's name. Any help will be greatly appreciated.
Upvotes: 2
Views: 103
Reputation: 3630
The way to convert it to a switch statement is as follows:
switch (browserName) {
case "MSIE":
break;
case "Safari":
break;
case "Chrome":
break;
case "IE11+":
break;
case "Firefox":
break;
default:
alert("Your browser is not compatible, please use one of these web browsers: Google Chrome, Mozilla Firefox, Internet Explorer or Apple Safari.");
}
and you could also do it this way:
switch (browserName) {
case "MSIE":
case "Safari":
case "Chrome":
case "IE11+":
case "Firefox":
default:
alert("Your browser is not compatible, please use one of these web browsers: Google Chrome, Mozilla Firefox, Internet Explorer or Apple Safari.");
}
but it would probably be better to do something like this:
function checkBrowser(browserName) {
var compatibleBrowsers = ['MSIE', 'Safari', 'Chrome', 'IE11+', 'Firefox'];
if (compatibleBrowsers.indexOf(browserName) !== -1) { return; }
alert("Your browser is not compatible, please use one of these web browsers: Google Chrome, Mozilla Firefox, Internet Explorer or Apple Safari.");
}
Upvotes: 1
Reputation: 101730
The values after case
should be the values that you want to match:
switch (browserName) {
case "MSIE":
// do something
break;
case "Safari":
// do something
break;
case "Chrome":
// do something
break;
case "IE11+":
// do something
break;
case "Firefox":
// do something
break;
default:
alert("test1");
break;
}
If all you care about is the case when the value doesn't match any of the known values, you can stack the case
s together:
switch (browserName) {
case "MSIE":
case "Safari":
case "Chrome":
case "IE11+":
case "Firefox":
break;
default:
alert("test1");
break;
}
Upvotes: 2
Reputation: 140
In a switch statement, you can't do:
case '1': == "MSIE"();
Instead do:
case 'MSIE':
//do code
break;
Hope it helps.
Upvotes: 1
Reputation: 527
Try this:
switch (browserName) {
case "MSIE": //Do MSIE work here
break;
case "Safari": //Do Safari work here
break;
case "Chrome": //Do Chrome work here
break;
case "IR11+": //Do IE11+ work here
break;
case "Firefox": //Do Firefox work here
break;
default:
alert("Browser Not Recognized!");
}
Upvotes: 1