bhb
bhb

Reputation: 2561

set doctype using javascript

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.

Upvotes: 4

Views: 10930

Answers (1)

S..
S..

Reputation: 5758

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.

Upvotes: 6

Related Questions