E
E
Enniack2015-09-09 01:00:37
Layout
Enniack, 2015-09-09 01:00:37

How to install a separate stylesheet for each browser?

For example, found on the Internet

<script type="text/javascript">
  if(window.opera) {
  document.write('<link rel="stylesheet" type="text/css" href="style/opera.css" />');
  }
  </script>

<script type="text/javascript">
  var detect = navigator.userAgent.toLowerCase();
  if((detect.indexOf('safari')) != -1) {
  document.write('<link href="style/safari.css" rel="stylesheet" type="text/css" />');
  }
  </script>

I try - does not work. Are there other ways for major browsers? And do I understand correctly that conditional comments do not work for ie10+?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qwetexac, 2015-09-09
@qwetexac

Try this option:

function get_name_browser(){
    var ua = navigator.userAgent;    
    if (ua.search(/Chrome/) > 0) return 'Google Chrome';
    if (ua.search(/Firefox/) > 0) return 'Firefox';
    if (ua.search(/Opera/) > 0) return 'Opera';
    if (ua.search(/Safari/) > 0) return 'Safari';
    if (ua.search(/MSIE/) > 0) return 'Internet Explorer';
    return 'Не определен';
}

var browser = get_name_browser();
if (browser = 'Google Chrome') {
    var css = document.createElement("LINK");
    css.rel = "stylesheet";
    css.href = "./file.css";
    document.getElementsByTagName("HEAD")[0].appendChild(css);
};
if (browser = 'Firefox') {
    //И так далее
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question