Safari, et al. browser detection syntax
Al
Status: New User - Welcome
Joined: 24 Mar 2006
Posts: 2
Location: Boston
Reply Quote
I've been struggling to make sure my syntax is correct to detect Safari and to load the appropriate stylesheet, but I'm not having any luck. I'm not much - if at all - a js programmer, so that's an issue :-\ Here's what I've got:

:: Code ::
<script type = "text/javascript" src="scripts/browser_detection.js"></script>
<script type="text/javascript">
function browser_css( ) {
   d = document;
   if ( ie6 ){
      d.write('<link rel = "stylesheet" type = "text\/css" href = "css/ie6.css" />');
   }
   else if ( saf ){
      d.write('<link rel = "stylesheet" type = "text\/css" href = "css/safari.css" />');
   }
   else {
      d.write('<link rel = "stylesheet" type = "text\/css" href = "css/global.css" />');
   }
}
</script>


There must be an error here...

Thanks for looking,
a!
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
Hi Al, although you've created the function browser_css(), you haven't called it.

So after the } you have to add this line:

:: Code ::
browser_css();


which would look like this:

:: Code ::
.....
   }
   else {
      d.write('<link rel = "stylesheet" type = "text\/css" href = "css/global.css" />');
   }
}
browser_css();

This code would have to occur in the <head> tag by the way, since that's where the link stuff is located.

If I were doing this, I'd just put the browser_css() function in the browser_detection.js library file, then just call the whole thing with the function browser_css();, which would change your code to this:

:: Code ::
<script type = "text/javascript" src="scripts/browser_detection.js"></script>
<script type="text/javascript">browser_css();</script>

and that's all. Any repeated code in general should always be pulled off the page and put into library files, for easier maintainance.
Back to top
Al
Status: New User - Welcome
Joined: 24 Mar 2006
Posts: 2
Location: Boston
Reply Quote
Hi Jeff,

It worked like a charm. Thanks for answering my js 101 question.

a!
Back to top
Display posts from previous:   

All times are GMT - 8 Hours