JavaScript in .XML file vs .HTML file

E

Erik Jälevik

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="http://teachwww.cogs.susx.ac.uk:8080/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>
 
M

Martin Honnen

Erik said:
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="http://teachwww.cogs.susx.ac.uk:8080/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.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top