IF ELSE STATEMENT

Hi

I have the following code (which I am assuming is JavaScript) in my html header

        <!--[if IE]>
            <link rel="stylesheet" href="css/stylesheet1.css" type="text/css" />
        <![endif]-->

This uses the style sheet stylesheet1 if internet explorer is detected.  Is it possible to somehow convert this small script into an if else statement?  So if it is ie use stylesheet1 else use stylesheet2?

Thanks
LVL 1
narmi2Asked:
Who is Participating?
 
sam85281Commented:
There is no ELSE for that, instead of using else you would just use multiple if/endif.

<!--[if IE]>
<link rel="stylesheet" href="css/stylesheet1.css" type="text/css" />
<![endif]-->
<!--[if !IE]>
<link rel="stylesheet" href="css/stylesheet2.css" type="text/css" />
<![endif]-->

Note the (!) before IE in the second one.

-Sam
0
 
smaccariCommented:
This code is not Javascript.
It's certainly a server side code, as this code is in HTML comments <!-- --> and thus not parsed by the browser.
0
 
nabsolCommented:
Hi

This is how you can do your job in javascript

<head>
<script language="JavaScript"><!--
browser_version= parseInt(navigator.appVersion);
browser_type = navigator.appName;
if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 4)) {
document.write("<link rel='stylesheet' href='css/stylesheet1.css' type='text/css' />");
}
else{
document.write("<link rel='stylesheet' href='css/stylesheet1.css' type='text/css' />");
}
// --></script>
</head>

By Nab
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
narmi2Author Commented:
Do you know anything else about that code?

for example can i use an "else" in there? or a <> or ! e.g.

        <!--[if IE]>
            <link rel="stylesheet" href="css/stylesheet1.css" type="text/css" />
        <![endif]-->
        <!--[if NOT IE]>
            <link rel="stylesheet" href="css/stylesheet2.css" type="text/css" />
        <![endif]-->

or
        <!--[if IE]>
            <link rel="stylesheet" href="css/stylesheet1.css" type="text/css" />
        <![endif]-->
        <!--[if <> IE]>
            <link rel="stylesheet" href="css/stylesheet2.css" type="text/css" />
        <![endif]-->

or
        <!--[if IE]>
            <link rel="stylesheet" href="css/stylesheet1.css" type="text/css" />
        <![endif]-->
        <!--[if ! IE]>
            <link rel="stylesheet" href="css/stylesheet2.css" type="text/css" />
        <![endif]-->

will any of these work?

If this is the wrong forum for this, where do you think I should ask this question?

Thanks
0
 
smaccariCommented:
Look at Nabsol solution, it's full JS and should work for you.
I do not know about your syntax though, never seen this ;)
0
 
narmi2Author Commented:
ok thanks

so what is this code called?

and does it have further syntax?

<!--[if IE]>
<![endif]-->
0
 
sam85281Commented:
They're called conditional comments.

Not much else to them you can use (!)Not equal to / (g)Greater than / (gte)Greaterthan-equal to / (l)Less than / (lte)Lessthan/eq to and you can designate version number.

Examples:
<!--[if g IE5]>
<![endif]-->

<!--[if lte IE6]>
<![endif]-->

etc...

-Sam
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.