tags:

views:

99

answers:

2

What is the syntax if I want to load a css file in my website with if statement.

Situation is like this.

If ie6 I will load ie6_style.css

and if ie7, mozilla, or new browsers, I will load style.css

A: 

You would need to detect which browser with javascript and then load the CSS.

Something like this

<script language="JavaScript"><!--
browser_version= parseInt(navigator.appVersion);
browser_type = navigator.appName;

if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 7)) {
document.write("<link REL='stylesheet' HREF='012899-ie7.css' TYPE='text/css'>");
}

else if (browser_type == "Netscape" && (browser_version >= 5)) {
document.write("<link REL='stylesheet' HREF='012899-netscape5.css' TYPE='text/css'>");
}

// --></script>
Dean
Thank you, this is working.
Jordan Pagaduan
This won't work if they have javascript disabled.
Malfist
Yes. That is a one problem of javascripts. I prefer to use css.
Jordan Pagaduan
+9  A: 
<link rel="stylesheet" type="text/css" href="style.css">
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="ie6_style.css">
<![endif]-->
RegDwight
More infomration can be found at quirksmode: http://www.quirksmode.org/css/condcom.html
Scott
You'll want to reverse the loading order, otherwise ie-specific styles will be overwritten by the normal styles.
Sean Vieira
Fixed the order, thanks Aberon.
RegDwight
thank you so much, it works.
Jordan Pagaduan