Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Setting CSS dynamically for Broswer

Setting CSS dynamically for Broswer
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Sep 13, 2003, 10:17 PM
 
Ohkay, if I want one CSS file for windows and another one for Macs, how can I achieve this? thanks
     
Senior User
Join Date: Mar 2002
Location: Chicago, IL
Status: Offline
Reply With Quote
Sep 14, 2003, 12:15 AM
 
The most reliable would be to have a normal style tag link to one of the files, and then use a javascript to sniff for the browser platform and if it's a certain platform then use a document.write() to add a link to that file. This will ensure that if a user has javascript off or your script chokes for some reason, something other than unstyled content will get displayed. The definitions in the second file will replace those in the first file, as long as you keep the same selectors in both.

Usually, though, you'd want to avoid browser sniffing unless it was really needed. Why exactly do you want separate css for each platform?
We need less Democrats and Republicans, and more people that think for themselves.

infinite expanse
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Sep 14, 2003, 11:06 AM
 
Because the CSS looks correct on Safari, IE for Mac, but when my Windows friends look at it, it looks like some big poop on a huge stick.

So, I just want the windows users to see a Not-as-cool-and-not-bad looking CSS. where can I get said WavaShrcipt?
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Sep 14, 2003, 02:29 PM
 
this works in safari and ie4win...

if it's a mac, it will get the mac_style.css, everything else gets win_style.css. the default.css is a catch-all if the browser doesn't run the script...

[php]
<html>

<head>

<link type="text/css" href="default.css" rel="stylesheet">

</head>

<body>


<script language="JavaScript" type="text/javascript">
<!--

if (navigator.appVersion.indexOf('Mac') != -1) {
document.write('<link type="text/css" href="mac_style.css" rel="stylesheet">');
} else {
document.write('<link type="text/css" href="win_style.css" rel="stylesheet">');
}

//-->
</script>


<a href="a.htm" class="link">asdf</a>

</body>

</html>
[/php]

kind of a weird way to do it... the script needs to be run in the body, document.write() won't work in the head.

EDIT:

my mistake... played around a little more and it looks like you can move the script up into the head and have everything work.... as long as the script is placed after the default stylesheet.
(Last edited by mzllr; Sep 14, 2003 at 05:17 PM. )
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Sep 14, 2003, 02:38 PM
 
.... and a little more straight-forward if you can use php...

[php]
<html>

<head>

<?

if (strstr($_SERVER['HTTP_USER_AGENT'], "Mac")) {

echo "<link href=mac_style.css rel=stylesheet type=text/css>";

} else {

echo "<link href=win_style.css rel=stylesheet type=text/css>";

}

?>

</head>

<body>

</body>

</html>
[/php]
(Last edited by mzllr; Sep 14, 2003 at 02:44 PM. )
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Sep 14, 2003, 02:39 PM
 
THanks a BUncher, duder!
That is some nice code, thank you
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Sep 14, 2003, 03:13 PM
 
Originally posted by mzllr:
this works in safari and ie4win...

if it's a mac, it will get the mac_style.css, everything else gets win_style.css. the default.css is a catch-all if the browser doesn't run the script...

[php]
<html>

<head>

<link type="text/css" href="default.css" rel="stylesheet">

</head>

<body>


<script language="JavaScript" type="text/javascript">
<!--

if (navigator.appVersion.indexOf('Mac') != -1) {
document.write('<link type="text/css" href="mac_style.css" rel="stylesheet">');
} else {
document.write('<link type="text/css" href="win_style.css" rel="stylesheet">');
}

//-->
</script>


<a href="a.htm" class="link">asdf</a>

</body>

</html>
[/php]

kind of a weird way to do it... the script needs to be run in the body, document.write() won't work in the head.


I tried using this in Safari and IE, and it does not switch to my prefered stylesheet:
A snippiet:
[php]
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
<title>
Prelude to Innovations in Motion
</title>
<link type="text/css" href="stylealt.css" rel="stylesheet" media = "screen">

</head>
<body>
__ <script language="JavaScript" type="text/javascript">

<!--
if (navigator.appVersion.indexOf('Mac') != -1) {

____document.write('<link type="text/css" href="style.css" rel="stylesheet" media = "screen">');

____}
else {
____document.write('<link type="text/css" href="stylealt.css" rel="stylesheet" media = "screen">');

____}


//-->

</script>




<img id="titletext" src="titletext.png" alt="Prelude to Innovations in Motion">

[/php]
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Sep 14, 2003, 03:41 PM
 
hmmmmm, seems to work here for me.

do you see a change on this one?
(Last edited by mzllr; Sep 14, 2003 at 03:49 PM. )
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Sep 14, 2003, 03:48 PM
 
yeah. i see the change.... hmmm....

EDIT:
I got it, i just copied the source, from the second page, thanks a lot, dude

if you want to see what i was working on:
http://dcohen.dyndns.org/~neb/blog%20prep/test.html
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Sep 14, 2003, 03:51 PM
 
[php]
__ <script language="JavaScript" type="text/javascript">

<!--
if (navigator.appVersion.indexOf('Mac') != -1) {

____document.write('<link type="text/css" href="style.css" rel="stylesheet" media = "screen">');

____}
else {
____document.write('<link type="text/css" href="stylealt.css" rel="stylesheet" media = "screen">');

____}


//-->

</script>
[/php]
are there underscores ( ___ ) in the code you are trying to run? if so, that could be a problem.
(Last edited by mzllr; Sep 14, 2003 at 05:14 PM. )
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Sep 14, 2003, 03:54 PM
 
Originally posted by Nebagakid:
yeah. i see the change.... hmmm....

EDIT:
I got it, i just copied the source, from the second page, thanks a lot, dude

if you want to see what i was working on:
http://dcohen.dyndns.org/~neb/blog%20prep/test.html
cool, i'm glad it worked out... cool site, too - i like the design.
(Last edited by mzllr; Sep 14, 2003 at 05:08 PM. )
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2