using document.createElementNS or document.createElement

Discussion in 'Javascript' started by James Black, May 21, 2006.

  1. James Black

    James Black Guest

    It appears that this is actually a difference between whether to use
    DOM2 or DOM1.

    I am trying to write my programs using XHTML for the webpage, but, I
    have to use DOM2 for xhtml 1.1.

    Now, how much of a problem will this be for browser compatibility?

    I am using a php script to change the mime type to be correct, either
    application/xhtml+xml for Mozilla based browsers, and html for the
    rest.

    It appears that IE 6 works fine this way.

    Is it wrong to just use namespaces as a norm?

    Thank you very much for any responses.
    James Black, May 21, 2006
    #1
    1. Advertising

  2. James Black

    Ian Collins Guest

    James Black wrote:
    > It appears that this is actually a difference between whether to use
    > DOM2 or DOM1.
    >
    > I am trying to write my programs using XHTML for the webpage, but, I
    > have to use DOM2 for xhtml 1.1.
    >

    You don't have to, DOM2 is a superset of DOM1.

    > Now, how much of a problem will this be for browser compatibility?
    >

    You are better to stick with DOM1, DOM2 support isn't that flash in IE.

    > I am using a php script to change the mime type to be correct, either
    > application/xhtml+xml for Mozilla based browsers, and html for the
    > rest.
    >
    > It appears that IE 6 works fine this way.
    >
    > Is it wrong to just use namespaces as a norm?
    >

    Why bother? If all of your document is in one namespace, why the added
    complexity? I haven't tried it, but I'd wager you will uncover all
    sorts of browser compatibility issues.

    --
    Ian Collins.
    Ian Collins, May 21, 2006
    #2
    1. Advertising

  3. James Black

    James Black Guest

    Ian Collins wrote:

    > > Is it wrong to just use namespaces as a norm?
    > >

    > Why bother? If all of your document is in one namespace, why the added
    > complexity? I haven't tried it, but I'd wager you will uncover all
    > sorts of browser compatibility issues.


    I have two or three namespaces actually, as I am mixing mathml, and
    perhaps svg, with my document.

    That is why I had to look at using xhtml 1.1.

    Thanx for the response.
    James Black, May 21, 2006
    #3
  4. On 21/05/2006 02:22, James Black wrote:

    [snip]

    > Now, how much of a problem will this be for browser compatibility?


    Depending on what you're doing, you may need to write two separate
    scripts: one for XHTML, one for HTML.

    > I am using a php script to change the mime type to be correct, either
    > application/xhtml+xml for Mozilla based browsers, and html for the
    > rest.


    There have been a lot of very poor implementations[1] of MIME
    type-oriented content negotiation that show complete disregard for RFC
    2616. I hope you aren't using one of them (and identifying Mozilla
    explicitly isn't particularly promising as other browsers support XHTML,
    too).

    [snip]

    Mike


    [1] XHTML or HTML 4
    Author: Michael Winter
    Group: alt.html
    Date: 2006-01-15 22:58:23 GMT
    Message-Id: jsAyf.1727$

    The issue of content negotiation stops at my final post in that thread
    (#45).

    <http://groups.google.co.uk/group/alt.html/browse_frm/thread/d3dee15034ffc6e7/c1d909527cecdbe2>

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, May 21, 2006
    #4
  5. James Black wrote:


    > I have two or three namespaces actually, as I am mixing mathml, and
    > perhaps svg, with my document.
    >
    > That is why I had to look at using xhtml 1.1.


    At least with Mozilla and with Opera you can only mix XHTML, SVG (and
    for Mozilla also MathML) elements in documents served with an XML MIME
    type like application/xml or text/xml or application/xhtml+xml. Then if
    you want to script elements you should always use the namespace aware
    methods of DOM Level 2 e.g.
    document.createElementNS('http://www.w3.org/1999/xhtml', 'p')
    for XHTML elements and e.g.
    document.createElementNS('http://www.w3.org/2000/svg', 'circle')
    for SVG elements as you are working with and building a namespace aware DOM.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 21, 2006
    #5
  6. James Black

    James Black Guest

    Michael Winter wrote:

    > There have been a lot of very poor implementations[1] of MIME
    > type-oriented content negotiation that show complete disregard for RFC
    > 2616. I hope you aren't using one of them (and identifying Mozilla
    > explicitly isn't particularly promising as other browsers support XHTML,
    > too).


    I will look at your link, but I look at the HTTP_ACCEPT header to
    decide which of the two mime types to return.

    It seems like the better way.

    Later I may have the application tune itself more by having the
    javascript check to see if the canvas is available, or is svg allowed,
    or use a drawing api, and do the same with mathml.

    It appears I will also need a switch to decide if I should use DOM1
    or DOM2 on a particular application.
    James Black, May 21, 2006
    #6
  7. James Black

    Ian Collins Guest

    James Black wrote:
    > Michael Winter wrote:
    >
    >
    >>There have been a lot of very poor implementations[1] of MIME
    >>type-oriented content negotiation that show complete disregard for RFC
    >>2616. I hope you aren't using one of them (and identifying Mozilla
    >>explicitly isn't particularly promising as other browsers support XHTML,
    >>too).

    >
    >
    > I will look at your link, but I look at the HTTP_ACCEPT header to
    > decide which of the two mime types to return.
    >
    > It seems like the better way.
    >
    > Later I may have the application tune itself more by having the
    > javascript check to see if the canvas is available, or is svg allowed,
    > or use a drawing api, and do the same with mathml.
    >
    > It appears I will also need a switch to decide if I should use DOM1
    > or DOM2 on a particular application.
    >

    I think you should check for specific DOM2 features and not use
    hasFeature(). Some UAs are less than honest in what they report.

    for example,

    if( element.addEventListener )...

    --
    Ian Collins.
    Ian Collins, May 21, 2006
    #7
  8. On 21/05/2006 21:01, James Black wrote:

    [snip]

    > I will look at your link, but I look at the HTTP_ACCEPT header to
    > decide which of the two mime types to return.
    >
    > It seems like the better way.


    It's the only (acceptable) way. However, 'looking at' the Accept header
    certainly isn't as simple as a substring match. It needs to parsed and
    the associated quality values (explicit and implicit) need to
    considered. It's in this latter area that content negotiation mechanisms
    seem to commit their worst offences.

    One also needs to consider misbehaviour from MSIE. When sending a
    request, it will issue an Accept header value similar to:

    image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

    As there is no quality value associated with the media range at the end
    of the list (*/*), it means that everything is acceptable; only the
    explicitly listed types have a higher preference[1]. For example,
    text/html is technically just as useful to IE as application/xhtml+xml,
    but of course that is far from the case. Similarly, when refreshing IE
    will only send:

    */*

    So, if a representation only matches using a media range, one should
    favour the most universally compatible type. As far as HTML versus XHTML
    is concerned, HTML is the obvious choice.

    If you have written this negotiation mechanism yourself, you should
    already have a very good working knowledge of HTTP. You /must/ have read
    at least sections 3.7 Media Types, 12 Content Negotiation, and 14.1
    Accept of the HTTP/1.1 specification [RFC2616] thoroughly and carefully.

    [snip]

    Mike


    [1] More specific media types have preference over less specific
    types and ranges. The HTTP/1.1 specification provides
    examples of this.

    [RFC2616] "Hypertext Transfer Protocol -- HTTP/1.1"
    <ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt>
    <http://www.rfc-editor.org/rfc/rfc2616.txt>

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, May 22, 2006
    #8
  9. James Black

    James Black Guest

    Michael Winter wrote:
    > If you have written this negotiation mechanism yourself, you should
    > already have a very good working knowledge of HTTP. You /must/ have read
    > at least sections 3.7 Media Types, 12 Content Negotiation, and 14.1
    > Accept of the HTTP/1.1 specification [RFC2616] thoroughly and carefully.


    I didn't write it myself, just modified it slightly to change what was
    sent to the client (such as the DOCTYPE).

    <?php
    $charset = "iso-8859-1";
    $mime = "text/html";
    function fix_code($buffer) {
    return (preg_replace("!\s*/>!", ">", $buffer));
    }
    if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {

    if(preg_match("/application\/xhtml\+xml;q=([01]|0\.\d{1,3}|1\.0)/i",$_SE
    RVER["HTTP_ACCEPT"],$matches)) {
    $xhtml_q = $matches[1];

    if(preg_match("/text\/html;q=q=([01]|0\.\d{1,3}|1\.0)/i",$_SERVE
    R["HTTP_ACCEPT"],$matches)) {
    $html_q = $matches[1];
    if((float)$xhtml_q >= (float)$html_q) {
    $mime = "application/xhtml+xml";
    }
    }
    } else {
    $mime = "application/xhtml+xml";
    }
    }
    if($mime == "application/xhtml+xml") {
    $prolog_type = "<?xml version=\"1.0\" encoding=\"$charset\"
    ?>\n<!DOCTYP
    E html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML
    2.0//EN\"\n\"http://www.w3.org
    /TR/MathML2/dtd/xhtml-math11-f.dtd\" [\n <!ENTITY mathml
    \"http://www.w3.org/199
    8/Math/MathML\">]> ";
    //
    if($mime == "application/xhtml+xml") {
    $prolog_type = "<?xml version=\"1.0\" encoding=\"$charset\"
    ?>\n<!DOCTYP
    E html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML
    2.0//EN\"\n\"http://www.w3.org
    /TR/MathML2/dtd/xhtml-math11-f.dtd\" [\n <!ENTITY mathml
    \"http://www.w3.org/199
    8/Math/MathML\">]>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">";
    } else {
    ob_start("fix_code");
    $prolog_type = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01
    Transition
    al//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html
    lang=\"en\">\n";
    }
    header("Content-Type: $mime;charset=$charset");
    header("Vary: Accept");
    print $prolog_type;
    ?>
    James Black, May 22, 2006
    #9
  10. On 22/05/2006 12:08, James Black wrote:

    [snip]

    > I didn't write it myself [...]


    Acknowledged.

    > if (stristr($_SERVER["HTTP_ACCEPT"],
    > "application/xhtml+xml")) {


    I would personally prefer the list to be parsed, but I suppose it will
    do (just barely). Besides that, I detest writing parsers (it always
    looks so messy) and it would be too much to post here.

    > if (preg_match(
    > "/application\/xhtml\+xml;q=([01]|0\.\d{1,3}|1\.0)/i",


    That regular expression is rather questionable and certainly doesn't
    obey the "be liberal in what you accept" principle.

    '/application\/xhtml\+xml\s*;\s*q\s*=\s*([01](?:\.\d*))/'

    The single quotes are important, by the way; don't change them to
    doubles without escaping the backslashes.

    [snip]

    > if (preg_match("/text\/html;q=q=([01]|0\.\d{1,3}|1\.0)/i",


    That pattern should undergo similar changes (particularly because it's
    broken).

    [snip]

    > } else {
    > $mime = "application/xhtml+xml";


    Once the regular expressions are fixed, this branch is more acceptable;
    it could have been taken only because the quality values didn't have the
    exact format specified. However, it would be better to make the q
    parameter optional and, if absent, use the implicit value of 1.0.


    In all, it's still not that good as it makes a lot of assumptions and
    doesn't consider things like type and sub-type ranges, nor implicit
    quality values. I wonder if there really are any /good/ implementations
    to be found on the Web?

    > if($mime == "application/xhtml+xml") {
    > $prolog_type = "<?xml version=\"1.0\" encoding=\"$charset\"
    > ?>\n<!DOCTYP
    > E html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML
    > 2.0//EN\"\n\"http://www.w3.org
    > /TR/MathML2/dtd/xhtml-math11-f.dtd\" [\n <!ENTITY mathml
    > \"http://www.w3.org/199
    > 8/Math/MathML\">]> ";
    > //
    > if($mime == "application/xhtml+xml") {
    > $prolog_type = "<?xml version=\"1.0\" encoding=\"$charset\"
    > ?>\n<!DOCTYP
    > E html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML
    > 2.0//EN\"\n\"http://www.w3.org
    > /TR/MathML2/dtd/xhtml-math11-f.dtd\" [\n <!ENTITY mathml
    > \"http://www.w3.org/199
    > 8/Math/MathML\">]>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">";


    Any particular reason why this is repeated?

    [snip]

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, May 23, 2006
    #10
  11. Michael Winter wrote:

    > In all, it's still not that good as it makes a lot of assumptions and
    > doesn't consider things like type and sub-type ranges, nor implicit
    > quality values. I wonder if there really are any /good/ implementations
    > to be found on the Web?


    Probably one could implement Apache's MIME Content Negotiation algorithm
    in PHP. It is free software and Open Source, after all :)


    PointedEars
    Thomas 'PointedEars' Lahn, May 25, 2006
    #11
    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. Nicolas VanOrton
    Replies:
    0
    Views:
    398
    Nicolas VanOrton
    Mar 8, 2005
  2. Tom Anderson

    createElementNS

    Tom Anderson, Jun 23, 2008, in forum: XML
    Replies:
    6
    Views:
    1,398
    Tom Anderson
    Jun 24, 2008
  3. Weston C

    document.createElement('a') doesn't work?

    Weston C, Jan 9, 2004, in forum: Javascript
    Replies:
    4
    Views:
    103
    Michael Hill
    Jan 9, 2004
  4. Pieter Van Waeyenberge

    'wrap' inconsistency? createElement VS document.write

    Pieter Van Waeyenberge, Feb 9, 2004, in forum: Javascript
    Replies:
    3
    Views:
    175
    Pieter Van Waeyenberge
    Feb 11, 2004
  5. Prasanna

    document.createElementNS on IE...

    Prasanna, Aug 8, 2007, in forum: Javascript
    Replies:
    3
    Views:
    313
    David Mark
    Aug 9, 2007
Loading...

Share This Page