How to create <!DOCTYPE> element in JavaScript?

A

Axel Dahmen

Hi,

I'd like to create a HTML document from scratch, but I don't know how to
create a "<!DOCTYPE HTML>" element.

Can someone please enlighten me?

Your help is appreciated.
Axel Dahmen
 
S

Sean Kinsey

Hi,

I'd like to create a HTML document from scratch, but I don't know how to
create a "<!DOCTYPE HTML>" element.

Can someone please enlighten me?

Your help is appreciated.
Axel Dahmen

The doctype attribute is readonly and cannot be changed after the
document has been parsed.
But if you are creating a dynamic document, then just use
document.write("<!doctype..."). An example can bee seen at
http://bolinfest.com/javascript/iframe_with_doctype.html
 
M

Martin Honnen

Axel said:
I'd like to create a HTML document from scratch, but I don't know how to
create a "<!DOCTYPE HTML>" element.

Can someone please enlighten me?

I don't think the W3C DOM API supports creating HTML 4 documents from
scratch but you can create an XHTML 1.0 document as follows (in browsers
like Mozilla, Opera, Safari):

var xhtmlNs = 'http://www.w3.org/1999/xhtml';

var doc = document.implementation.createDocument(xhtmlNs, 'html',
document.implementation.createDocumentType('html', '-//W3C//DTD XHTML
1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'));

var head = doc.documentElement.appendChild(doc.createElementNS(xhtmlNs,
'head'));
var title = head.appendChild(doc.createElementNS(xhtmlNs, 'title'));
title.appendChild(doc.createTextNode('Example'));

// add further nodes here
 
A

Axel Dahmen

Sean Kinsey said:
The doctype attribute is readonly and cannot be changed after the
document has been parsed.
But if you are creating a dynamic document, then just use
document.write("<!doctype..."). An example can bee seen at
http://bolinfest.com/javascript/iframe_with_doctype.html

I understand... - Great, thanks a lot for the hint! I was so focussed on DOM
manipulation that I didn't even think of this straight-forward approach.

Cheers,
Axel Dahmen
 
T

Thomas 'PointedEars' Lahn

Sean said:
The doctype attribute is readonly and cannot be changed after the
document has been parsed.

There is no "doctype attribute" in implementations. There is a `doctype'
property.
But if you are creating a dynamic document, then just use
document.write("<!doctype..."). An example can bee seen at
http://bolinfest.com/javascript/iframe_with_doctype.html

Another possibility is using a `data:' URI for the location of a Window.


PointedEars
 
A

Axel Dahmen

Ah, I see... Excellent! That's what I was searching for in the first place.

Thanks a lot!
Axel Dahmen


----------------------------
 
A

Axel Dahmen

Another possibility is using a `data:' URI for the location of a Window.

I see... Interesting approach. But I can't use this with IE8, I'm afraid.

Cheers,
Axel Dahmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top