PDA

View Full Version : <!DOCTYPE... keeps my javascript from working.


jerrykobes
12-22-2008, 03:12 PM
I entered this DOCTYPE:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


which I have been using since reading my CSS book. However, when I used the following code from my Javascript book with that DOCTYPE above it prevented the script from working:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>JavaScript Loan Calculator</title>
<style>
.result {
font-weight:bold;
}
#payment {
text-decoration: underline;
}
</style>
</head>
<body>
<form name="loandata">
<table>
<tr><td><b>Enter Loan Information:</b></td></tr>
<tr>
<td>1) Amount of the loan (any currency):</td>
<td><input type="text" name="principal" onchange="calculate();"></td>
</tr>
<tr>
<td>2) Annual percentage rate of interest:</td>
<td><input type="text" name="interest" onchange="calculate();"></td>
</tr>
<tr>
<td>3) Repayment period in years:</td>
<td><input type="text" name="years" onchange="calculate();"></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Compute" onlick="calculate();"></td>
</tr>
<tr><td><b>Payment Information</b></td>/tr>
<tr>
<td>4) Your monthly payment:</td>
<td>$<span class="result" id="payment"></span></td>
</tr>
<tr>
<td>5) Your total payment:</td>
<td>$<span class="result" id="total"></span></td>
</tr>
<tr>
<td>6) Your total interest payments:</td>
<td>$<span class="result" id="totalinterest"></span></td>
</tr>
</table>
</form>

<script language="Javascript">
function calculate() {
var principal = document.loandata.principal.value;
var interest = document.loandata.interest.value / 100 / 12;
var payments = document.loandata.years.value * 12;

var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);

var payment = document.getElementById("payment");
var total = document.getElementById("total");
var totalinterest = document.getElementById("totalinterest");

if (isFinite(monthly)) {
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (monthly * payments).toFixed(2);
totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2);
}

else {
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
}
}
</script>
</body>
</html>


However, it runs fine I remove that DOCTYPE statement. Why did this happen and what is the proper DOCTYPE to use. Also, a bit less important but still annoying, whenever I open this html/javascript I get this at the top "/tr>" and I can't find any incomplete tag in my code anywhere. Why does it keep on appearing?

coothead
12-22-2008, 03:16 PM
Hi there jerrykobes,

do you think that you may possibly have forgotten something. :disagree:

jerrykobes
12-22-2008, 03:31 PM
Even if I did, is there a more up to date "DOCTYPE" that I should be using now since my book is outdated? Is it still xhtml 1.0?

I know I would learn more by trying to dissect this DOCTYPE stuff myself but right now my only motivation is to keep on reading this Javascript and this is holding me up.

I would appreciate a quick complete answer from anyone.

althalus
12-22-2008, 03:32 PM
<tr><td><b>Payment Information</b></td>/tr>Found the incomplete tag for ya. :-]

jerrykobes
12-22-2008, 03:39 PM
Lol, so that's where it was hiding!

coothead
12-22-2008, 03:45 PM
Hi there jerrykobes,

I found that the script worked OK in IE7, Safari 3.1, Opera 9.3 and Firefox 3.0.5. :agree:
As for The DOCTYPE...

You're better off not pretending that this is XHTML, really.
If you're serving it as "text/html" then it is, for all practical purposes, HTML, not XHTML.
Not pretending the document is XHTML and instead using HTML4.01 makes the document smaller,
with no ill effects at all.
HTML4.01 and XHTML1.0 are practically identical in what elements they allow and what their element
content models are, but they are not compatible in some ways with regards to how empty elements are
handled, with regards to style sheets, with regard to the DOM, and with regards to stuff like CDATA blocks
and with regards to case sensitivity.


Beware of XHTML (http://www.webdevout.net/articles/beware-of-xhtml)


The rogue /tr> and similar errors can be discovered and rectified if you use the Validator...
http://validator.w3.org/
...which shows 33 errors for your page.

Horus_Kol
12-22-2008, 08:35 PM
also, with the <script> tags, you should be specifying the language like:


<script type="text/javascript">


although most browsers will recognise the language attribute