0

I have this code in jquery to add diferent css styles according to the browser since i can't get stuff in the exact same place. This is the code that adds the link to the style sheet when the user is using chrome.

<script type= "text/javascript">
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
    if ($.browser.chrome) {
        document.write('<link rel="stylesheet" href="css/chrome.css" 
                                         type="text/css" media="screen">'); 
    }
</script>

However, when i use chrome, another code (the same one that adds the link to the style sheet for safari) also becomes true and adds a link too. so i have two stylesheets when im using chrome. Both codes are exact the same thing, the only difference is that the word "chrome" is changed for "safari".

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
Ale Rojas
  • 497
  • 2
  • 7
  • 17
  • Why do you need different stylesheets for both Chrome & Safari in any case? We host and manage over 1500 websites but not one of them makes use of a safari-specific website. Because they both use the same layout-engine (webkit), the same tweaks apply to both browsers. – ClarkeyBoy Mar 18 '13 at 07:10

3 Answers3

1

For better, more accurate chrome detection, check out following url: https://stackoverflow.com/a/11382806/2177992

Besides that you can structure your code like this:

if ( chrome )
{
    //chrome stuff
}
else if ( safari )
{
    //safari stuff
}

This way the both will never be active at the same time.

Community
  • 1
  • 1
n0tiz
  • 352
  • 1
  • 4
  • Thank You very much. im new here and some of the answers were kind of techy but im sure they are helpful! This one really solved the issue. – Ale Rojas Mar 17 '13 at 21:00
0
"mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.22 (khtml, like gecko) chrome/25.0.1364.172 safari/537.22"

The Chrome useragent string has safari in it so when you check to see if safari is in the string, it'll be true for both Chrome and Safar.

See these questions for a better way to detect browsers

Browser detection in javascript?

Detect version of browser

Interesting relevant article: History of the browser user-agent string

Community
  • 1
  • 1
sachleen
  • 30,730
  • 8
  • 78
  • 73
0

This would be because navigator.userAgent.toLowerCase() contains safari. My version of that is mozilla/5.0 (windows nt 6.1) applewebkit/537.22 (khtml, like gecko) chrome/25.0.1364.172 safari/537.22.

According to http://user-agent-string.info/?Fuas=mozilla%2F5.0+%28windows+nt+6.1%29+applewebkit%2F537.22+%28khtml%2C+like+gecko%29+chrome%2F25.0.1364.172+safari%2F537.22&test=4483&action=analyze this part of the string is called a browser signature.

ClarkeyBoy
  • 4,934
  • 12
  • 49
  • 64