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.
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.
I hope this one might help some of you ( Tested in Console and it changes actual DOCTYPE)
var newDoctype = document.implementation.createDocumentType(
'html',
'-//W3C//DTD XHTML 1.0 Transitional//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdd'
);
document.doctype.parentNode.replaceChild(newDoctype,document.doctype);
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():
createDocumentType
is not available in all browsers though. Chromium doesn't have it.
May 30, 2011 at 23:17
To try to justify this use case, I have the following scenario:
I have a TAL template that renders a small portition of the page. I then wrap that portition in parent tags like this:
<html tal:omit-tag="True" ...>
<body tal:omit-tag="True">
<div class="wrapper" tal:omit-tag="True">
..
<div id="mydiv" tal:content="foo()">Example content.</div>
..
</div>
</body>
</html>
This way, this TAL template is viewable/editable as a stand-alone HTML file, by a designer. One cannot omit the DTD in TAL though, so it cannot be added there.
An easy way to add it with JavaScript is like this:
if (!document.doctype) {
document.write('<!doctype HTML>\n' + \
document.head.outerHTML + \
document.body.outerHTML);
}
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.
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.
If the problem is only with IE, As I also experienced the same problem, I used quirks Mode for IE - Just use a comment before DOC TYPE declaration and IE will go into quirks mode.
The other way that you can work around is first to load a script to detect the browser and then redirect with the browsers parameter to the other page where you can declare Browser dependent doctype.
example of what I have done with my code is like :-
<!--[if lt IE 9]>
<![endif]-->
<![if gte IE 9]>
<!DOCTYPE html>
<![endif]>
Here I have removed the doctype declearation from the browe