4

I have a html page with no doctype declared deployed to a server(say A). This is fetching js files from another server(say B). js creates necessary html page to display. Now IE8 is creating problems as there is not doctype declared(sets itself to IE5 quirks mode)

Now doctype is the first line read, and this seems impossible to be done this way(using js to set the doctype). Is it possible to set a meta tag instead to set the page to standards mode? Or is there any other i can set the page to standard page without modifying the html page from server A.

1

1 Answer 1

6
var nodeDoctype = document.implementation.createDocumentType(
 'html',
 '-//W3C//DTD XHTML 1.0 Transitional//EN',
 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdd'
);
if(document.doctype) {
    document.replaceChild(nodeDoctype, document.doctype);
} else {
    document.insertBefore(nodeDoctype, document.childNodes[0]);
}

Update based on your comment:

It is possible to change the doctype with JS to enable compatability viewing (as done here: http://www.webmasterworld.com/forum91/4856.htm) but is quite a nasty hack and not recommended. Ideally you can do this server side. So have a doctype js parameter and then do a page reload:

window.location = window.location+"?doctype=newdoctype"

This will cause the page to reload which might not suit you, but is the safest way to do it.

Sign up to request clarification or add additional context in comments.

5 Comments

will the browser reread the doctype. the html page is already loaded.
btw document.doctype is null
how do i do this in IE. createDocumentType is not avaialable in <IE9
Interesting. I was tempted to say this answer is completely wrong since the DOCTYPE is actuall used by the XML parser to determine how to interpret and validate the (X)HTML/XML markup, thus it's useless to modify an already parsed DOM to change the doctype. But it's IE and might really work. "A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes." Wikipedia: DOCTYPE
The browser will not reread the doctype if you add it with javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.