views:

74

answers:

4

anyone here know how i can dynamically change the doctype with javascript?

i have tried with this function,

document.doctype('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'); ,

but it does not work.

A: 

See:

Using JS to change the doctype

Sarfraz
+1  A: 

I don't think you can. doctype is listed as a property in the W3C documentation, but it's read-only. Even if it weren't, I can't imagine what effect changing it would have in real-world browsers.

Re your subsequent comments: You'd have to handle this server-side and serve back the page tailored to the target browser. But you shouldn't have to do that in any but some very fringe cases.

T.J. Crowder
@T.J. Crowder: It is read-only property :(
Sarfraz
@sAc: Just got there. :-)
T.J. Crowder
About your last sentence (“they just flag quirks mode or not depending on whether there *is* a doctype, not what it is”): that's not true. Several Doctypes will trigger quirks mode, like the HTML 3.2 one the OP mentioned in one of his comments to his question. See [Activating Browser Modes with Doctype](http://hsivonen.iki.fi/doctype/#handling).
Marcel Korpel
@Marcel: Fascinating. I've seen the statement "it doesn't matter, as long as there's a DOCTYPE" so many times, I'm astonished not to have seen it contradicted before. Very interesting table, thanks. And you can see why 3.2 *would* be quirks mode, too. I've removed the sentence, thanks again.
T.J. Crowder
A: 

document.doctype is a read-only property, not method, apparently according to MDC.

What you need is:

https://developer.mozilla.org/En/DOM/DOMImplementation.createDocumentType

Returns a DocumentType object which can either be used with DOMImplementation.createDocument upon document creation or they can be put into the document via Node.insertBefore() or Node.replaceChild():

Lajla
A: 

Even if you could, your code would be executing after the page has already decided to render by which case the effect of changing the doctype is nothing. The only way I can imagine modern browsers having doctype issues is that you are relying on quirks mode in IE - fix your design to work for all browsers but IE, then look at IE specific styling.

Graphain