简体   繁体   中英

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?

conditional comments "work" in markup, you can wrap them around CSS, HTML and JavaScript. Just wrap whatever for non IE with [if !IE]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM