DOCTYPE element in HTML
Dear all how can i set the DOCTYPE element in HTML to be dynamic depending on the browser bei开发者_开发百科ng used?
Why do you want to do this? It's best if you solve the root problem. HTML is meant to be served as static data which means that if you add caching at some point then the whole "content negotiation" trick will fail. Also note that browser user agent sniffing is hard to do 100% correct and you'll need to keep updating your logic as time goes by (and new browsers and versions are released).
Guess you had some troubles on page display, so you decide to change to doctype potentially to quirks mode or html4 standard? If this is the case, I'd still recommend you spend your time on tweak styles better than changing the whole page doctype.
You don't. You just use <!doctype html>
. Doctypes are only used to determine whether to use standards mode; changing them is just a waste of resources.
It's not possible using only client side code.
If you don't have server side language at your disposal, build several "versions" of each page, each with different doctype then redirect using JavaScript to proper page according to browser. If you choose this path let me know and I can come with sample code.
Best solution is to use <!doctype html>
it is a standards doctype and is now supported in all browsers since HTML5 is backwards-compatible
Plus with more and more HTML5 support being added by the browsers, it's time to prepare for the change.
精彩评论