Solved

OS detection script for CSS link tag - is this correct syntax?

Posted on 2002-03-08
8
414 Views
Last Modified: 2013-11-19
a quickie for who ever gets it first:

Is this the correct syntax for conditionally including a link to a mac and PC stylesheet.

<SCRIPT LANGUAGE="JavaScript">
<!--
version = navigator.appVersion;
if (version.toLowerCase().indexOf("mac")!=-1) {
document.write('<link rel="STYLESHEET" type="text/css" href="/styles_mac.css">');
}
else {
document.write('<link rel="STYLESHEET" type="text/css" href="/styles.css">');
}
// -->
</script>
0
Comment
Question by:drstuey
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6852403
if (navigator.userAgent.match(/Mac/i)) {
 document.write('<link rel="STYLESHEET" type="text/css" href="/styles_mac.css">');
} else {
 document.write('<link rel="STYLESHEET" type="text/css" href="/styles.css">');

}
0
 

Author Comment

by:drstuey
ID: 6852474
Thanks, I have implemented that, but I won't know if that worked until I run it through Browser Photo tommorrow.

I was hoping for an explanation as well. I was quite happy with the OS detection part of the script, the explanation I read for doing it this way:

version = navigator.appVersion;
if (version.toLowerCase().indexOf("mac")!=-1)

seemed entirely reasonable to me. It was as much the syntax of everything else I was concerned about. The version I posted doesn't seem to be doing anything, it seems like both CSS links are visible to all browsers so the Macs see the PC CSS link as well and that overrides the Mac one????
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6852485
it is the wrong property that you are using, to get the Mac platform, it would be safer to use the userAgent property.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:drstuey
ID: 6852591
Thanks, I have implemented that, but I won't know if that worked until I run it through Browser Photo tommorrow.

I was hoping for an explanation as well. I was quite happy with the OS detection part of the script, the explanation I read for doing it this way:

version = navigator.appVersion;
if (version.toLowerCase().indexOf("mac")!=-1)

seemed entirely reasonable to me. It was as much the syntax of everything else I was concerned about. The version I posted doesn't seem to be doing anything, it seems like both CSS links are visible to all browsers so the Macs see the PC CSS link as well and that overrides the Mac one????
0
 

Author Comment

by:drstuey
ID: 6852593
ooops, I must have gone back in my browser and reposted my comment
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 77 total points
ID: 6855298
drstuey, has it been successful?
0
 
LVL 12

Expert Comment

by:ahosang
ID: 8076107
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

points to b1xml2
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
0
 

Author Comment

by:drstuey
ID: 8077068
Naughty me I have been prompted to give the points out.

Yes it works. It was my mistake that I thought it didn't.

cheers
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question