问题
So I have this XHTML document where I have this script embedded in between the <head> tags:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--Created January 12, 2016-->
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--Date updated, courtesy of -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function makeArray() {
for (i = 0; i < makeArray.arguments.length; i++)
this[i] = makeArray.arguments[i];
}
function getFullYear(d) {
var y = d.getYear();
if (y < 1000) {
y += 1900
};
return y;
}
var days = new makeArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var months = new makeArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
function format_time(t) {
var Day = t.getDay();
var Date = t.getDate();
var Month = t.getMonth();
var Year = getFullYear(t);
timeString = "";
timeString += days[Day];
timeString += ", ";
timeString += months[Month];
timeString += " ";
timeString += Date;
timeString += ", ";
timeString += Year;
return timeString;
}
m = new Date(document.lastModified);
d = new Date();
$(function() {
$('.timestamp').html(format_time(m))
});
</script>
</head>
that updates the date of when the document was last updated in the footer:
<div id="copyright">
<p id="copyright-invert">
Copyright ©
<!-- function for year change -->
<script type="text/javascript">
now=new Date();
year=now.getFullYear();
</script>
<script type="text/javascript">
document.write(year);
</script>
| All Rights Reserved
<br/>
UPDATED: <span class="timestamp"></span>
</p>
</div>
</body>
</html>
The issue here is when I put the document through the validator, it comes up with a couple of errors and warnings as provided below:
I am not sure what do to correct this so the document can pass validation . . .
回答1:
The best way to deal with this, as far as validation is concerned, is to not use XHTML.
- XHTML is more trouble than it is worth for 99%+ of cases (since most people trying to use XHTML tell browsers it is HTML and thus have to write it so it works with both XML and HTML parsers).
- Transitional Doctypes were designed to help ease people into using CSS while CSS support was poor. That was in the 90s. CSS support hasn't been poor for a very, very long time.
- The current standard is HTML 5. Browsers support it better than they do XHTML 1.0. You should almost certainly be using that (and not the XHTML serialisation of it either).
If you really, really want to use XHTML then read:
- Differences from HTML 4.01: Script and Style Elements
- XHTML Media Types: Compatibility Guidelines
Which say, to paraphrase, the best way to make scripts valid in XHTML is to put them in an external JS file.
Failing that, wrap the inside of the script element with comments and CDATA markers:
<script>//<![CDATA[
...
//]]></script>
来源:https://stackoverflow.com/questions/36073313/last-updated-javadscripting-in-xhtml