How to create <!DOCTYPE> element in JavaScript?

Discussion in 'Javascript' started by Axel Dahmen, Apr 3, 2010.

  1. Axel Dahmen

    Axel Dahmen Guest

    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
     
    Axel Dahmen, Apr 3, 2010
    #1
    1. Advertising

  2. Axel Dahmen

    Sean Kinsey Guest

    On Apr 3, 3:26 pm, "Axel Dahmen" <> wrote:
    > 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
     
    Sean Kinsey, Apr 3, 2010
    #2
    1. Advertising

  3. Axel Dahmen wrote:

    > 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


    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Apr 3, 2010
    #3
  4. Axel Dahmen

    Axel Dahmen Guest

    "Sean Kinsey" <> schrieb im Newsbeitrag
    news:...
    > On Apr 3, 3:26 pm, "Axel Dahmen" <> wrote:
    > 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
     
    Axel Dahmen, Apr 3, 2010
    #4
  5. Sean Kinsey wrote:

    > "Axel Dahmen" wrote:
    >> 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?

    >
    > 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
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Apr 3, 2010
    #5
  6. Axel Dahmen

    Axel Dahmen Guest

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

    Thanks a lot!
    Axel Dahmen


    ----------------------------
    "Martin Honnen" <> schrieb im Newsbeitrag
    news:4bb748ab$0$6770$-online.net...
    > Axel Dahmen wrote:
    >
    >> 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
    >
    >
    > --
    >
    > Martin Honnen
    > http://msmvps.com/blogs/martin_honnen/
     
    Axel Dahmen, Apr 3, 2010
    #6
  7. Axel Dahmen

    Axel Dahmen Guest

    "Thomas 'PointedEars' Lahn" <> schrieb im Newsbeitrag
    news:...

    > 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
     
    Axel Dahmen, Apr 3, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abs
    Replies:
    0
    Views:
    571
  2. O.B.
    Replies:
    2
    Views:
    3,325
    Damian Brunold
    Feb 7, 2006
  3. Replies:
    2
    Views:
    644
  4. Iulian Ilea

    problem: create element in create element

    Iulian Ilea, Dec 14, 2006, in forum: Javascript
    Replies:
    4
    Views:
    187
    Iulian Ilea
    Dec 17, 2006
  5. Larry Lindstrom
    Replies:
    19
    Views:
    1,307
    Jonathan N. Little
    Jun 12, 2012
Loading...

Share This Page