Board index » javascript » JavaScript in .XML file vs .HTML file

JavaScript in .XML file vs .HTML file

2003-11-06 05:22:37 AM
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.taxform;
// Check that required fields are not empty
if (tf.fname.value == "" ||
tf.sname.value == "") {
// do stuff
}
</script>
...
<form
id="taxform"
action="teachwww.cogs.susx.ac.uk/egj20/servlet/TaxReturnServlet"
method="post"
onsubmit="return validate()">
<p>
<label for="fname" class="required">First name:</label>
<input id="fname" name="fname" type="text" size="40" maxlength="40" />
</p>
...
</form>
-
 

Re:JavaScript in .XML file vs .HTML file

Erik Jälevik wrote:
Quote
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.taxform;

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

</script>

...

<form
id="taxform"

action="teachwww.cogs.susx.ac.uk/egj20/servlet/TaxReturnServlet"
method="post"
onsubmit="return validate()">

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

...

</form>
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
JavaScript.FAQTs.com/
-