JavaScript in .XML file vs .HTML file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Jälevik

    JavaScript in .XML file vs .HTML file

    I'm trying to understand the difference between using JavaScript in XML and
    XHTML. I have a valid XHTML file with a form and some JavaScript. If I save
    it as a .html file (this is under Windows), it all works fine, but if I save
    it as a .xml file, the script can no longer reference the form.

    In the code extract below, when run as xml in Mozilla throws an error on the
    line trying to access tf.fname.value saying "tf has no properties". Why is
    this?

    <script type="text/javascript">

    function validate() {

    tf = document.taxfor m;

    // Check that required fields are not empty
    if (tf.fname.value == "" ||
    tf.sname.value == "") {
    // do stuff
    }

    </script>

    ....

    <form
    id="taxform"

    action="http://teachwww.cogs.s usx.ac.uk:8080/egj20/servlet/TaxReturnServle t"
    method="post"
    onsubmit="retur n validate()">

    <p>
    <label for="fname" class="required ">First name:</label>
    <input id="fname" name="fname" type="text" size="40" maxlength="40" />
    </p>

    ....

    </form>


  • Martin Honnen

    #2
    Re: JavaScript in .XML file vs .HTML file



    Erik Jälevik wrote:
    [color=blue]
    > I'm trying to understand the difference between using JavaScript in XML and
    > XHTML. I have a valid XHTML file with a form and some JavaScript. If I save
    > it as a .html file (this is under Windows), it all works fine, but if I save
    > it as a .xml file, the script can no longer reference the form.
    >
    > In the code extract below, when run as xml in Mozilla throws an error on the
    > line trying to access tf.fname.value saying "tf has no properties". Why is
    > this?
    >
    > <script type="text/javascript">
    >
    > function validate() {
    >
    > tf = document.taxfor m;
    >
    > // Check that required fields are not empty
    > if (tf.fname.value == "" ||
    > tf.sname.value == "") {
    > // do stuff
    > }
    >
    > </script>
    >
    > ...
    >
    > <form
    > id="taxform"
    >
    > action="http://teachwww.cogs.s usx.ac.uk:8080/egj20/servlet/TaxReturnServle t"
    > method="post"
    > onsubmit="retur n validate()">
    >
    > <p>
    > <label for="fname" class="required ">First name:</label>
    > <input id="fname" name="fname" type="text" size="40" maxlength="40" />
    > </p>
    >
    > ...
    >
    > </form>[/color]

    If you want to use XHTML then you need to serve it with a Content-Type
    header of
    application/xhtml+xml
    I think at least on Windows Mozilla assumes that content type if the
    file has a suffix of
    .xhtml
    With that suffix and a recent Mozilla release (1.4 or later) the HTML
    DOM should be defined.
    If you use the suffix .xml then the content type is text/xml and that
    can be any XML so the DOM will be W3C DOM Core and XML only.

    --

    Martin Honnen


    Comment

    Working...