Search code examples
phpjavascripthtmlinternet-explorerconditional-comments

Do conditional comments only work for stylesheets?


Do conditional comments only work for CSS styles and stylesheet links, or can they be applied to all HTML/JS. I'm asking because I'd like to display a specific message if the user is using a browser in which some functionality is known not to work/function as well as it could in others?

If not, is there anyway, via PHP + regex to get the exact browser that the user is using, as $_SERVER['HTTP_USER_AGENT'] seems to return a string containing several user agent names?


Solution

  • If you mean stuff like:

    <!--[if lte IE 6]> ... <![endif]-->
    

    yes, you can use this in HTML too (eg. it is often used to conditionally add some extra CSS for IE6 fixes).

    But I wouldn't rely on this in order to, let's say, tell the user to upgrade the outdated browser. Instead, I would do that on the server-side by checking the $_SERVER['HTTP_USER_AGENT'].

    The browser recognition isn't straight-forward since browsers tend to format the user-agent as they like; there are many libraries that use comparisons lists etc. in order to try figure out which actually is the browser/version/os by parsing the user-agent string.