...

View Full Version : Help with Javascript



Krudo
02-03-2005, 11:41 AM
Hi all

I am a n00b to this site and JavaScript and thought this would work but for some reason I think it needs a source within the <body></body> otherwise there is nothing to tell the script to work or am I completely barking up the wrong tree...

What I'm after is Microsoft and Netscape, Mozilla, Firefox and so on to detect the browser the user is using when viewing the site, therefore showing the correct CSS file for the correct browser. Neat yeah...? But when I view the page none of the CSS will work...

Any aides please... :confused:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Under Development - Coming Soon</title>
<script type="text/javascript" language="javascript">
<!--
if (navigator.appName == 'Microsoft
Internet Explorer') {
document.write('<link rel="stylesheet" type="text/css" href="images/blue/style.css">')
}
else {
document.write('You are running either Mozilla, Firefox or Netscape?')
}
}
// -->
</script>
</head>

<body background="images/blue/background.gif">

</body>
</html>

rmedek
02-03-2005, 12:04 PM
Hello, welcome to the forums. You might want to take another look at the posting guidelines (http://www.codingforums.com/postguide.htm); "help with Javascript" isn't really a helpful title in a javascript forum. :)

I'm a complete JS idiot, but you can achieve something very similar using IE's conditional comments:


<link rel="stylesheet" media="screen" href="css/style.css" /> <-- this stylesheet is read by all browsers, including nice, standards compliant ones

<!--[if IE]> <-- this feeds a stylesheet specifically to Internet Explorer, so you can serve all your CSS hacks to only IE
<link rel="stylesheet" media="screen" href="css/iestyle.css" />
<![endif]-->


Maybe this is an alternative?

Krudo
02-03-2005, 12:16 PM
I done it... :thumbsup:


It's working... :D

it was because I put a --> Microsoft

Its should look like this instead... works a treat

<script type="text/javascript" language="javascript">
<!--
if (navigator.appName == 'Microsoft Internet Explorer') {
document.write('<link rel="stylesheet" type="text/css" href="images/blue/style.css">')
}
else {
document.write('You are running either Mozilla, Firefox or Netscape?')
}
// -->
</script>

and now it brings up the message for Mozilla... All I have to do now is write a CSS file to be compatible with Mozilla, Firefox, Netscape, Opera... man that will be some work...

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum