View Full Version : <link> and <script> tags and XHTML
Please could someone take a look at these two methods of calling "cookies.js" and tell me why the second is not working.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<script type="text/javascript" src="js/cookies.js" />
</head>
<body onload="checkCookie()"></body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<link type="text/javascript" href="js/cookies.js" />
</head>
<body onload="checkCookie()"></body>
</html>
I was under the impression that the <link> tag was the more appropriate. I recall reading something about it in an XHTML tutorial but can't seem to make it work.
Can anyone shed any light on this.
Thanks,
dog
mwinter
11-30-2006, 04:52 PM
<script type="text/javascript" src="js/cookies.js" />
If this was served as XML (application/xhtml+xml, text/xml, or application/xml), then it would be processed properly as an empty-element start-tag. However, you undoubtedly served it as HTML, therefore browsers will be looking for an explicit end-tag.
Don't serve XHTML as HTML. Use HTML 4.01.
If must use XHTML, and I seriously doubt that, it may only be XHTML 1.0, it should use the Strict DTD (the same applies to any new HTML document), and it must conform to Appendix C of the XHTML 1.0 Recommendation.
I was under the impression that the <link> tag was the more appropriate.
The link element can be used to link to style sheets, and specifying relationships to other documents, but not for including scripts.
Mike
Mike,
Thanks a lot for the quick response.
Don't serve XHTML as HTML. Use HTML 4.01.
Just to clarify, the script tag needs to be closed like this:
<script type="text/javascript" src="js/cookies.js"></script>
and not like this:
<script type="text/javascript" src="js/cookies.js" />
Is that correct?
If must use XHTML, and I seriously doubt that, it may only be XHTML 1.0, it should use the Strict DTD (the same applies to any new HTML document), and it must conform to Appendix C of the XHTML 1.0 Recommendation.
I'll spend some more time reading on w3schools.com (http://w3schools.com) as I'm confused about what DOCTYPE is most apprioriate to use, when and why. And also about the ways that I can and cannot close tags.
What are the practical implications of making mistakes regarding this and how can I check that I'm doing it right?
mwinter
11-30-2006, 07:12 PM
Just to clarify, the script tag needs to be closed like this:
<script type="text/javascript" src="js/cookies.js"></script>
and not like this:
<script type="text/javascript" src="js/cookies.js" />
Is that correct?
Yes, exactly.
I'll spend some more time reading on w3schools.com (http://w3schools.com) as I'm confused about what DOCTYPE is most apprioriate to use, when and why.
In general, use the Strict document types. Transitional document types should be reserved for legacy documents that aren't going to be completely rewritten to avoid deprecated elements and attributes.
And also about the ways that I can and cannot close tags.
For any element that can have content, whether is does or not, include an end-tag. Any element that has an EMPTY content model - for example, the link, br, and img elements - should end ">" (HTML) or "/>" (XHTML).
What are the practical implications of making mistakes regarding this
That depends on the mistake. However, all mistakes should be avoided: invoking the error correction mechanism implemented by browsers can upset the document tree. Not all browsers correct problems in the same way and this may resulting in unexpected rendering or script errors.
and how can I check that I'm doing it right?
The W3C validator (http://validator.w3.org/) can check that (X)HTML and XML documents are valid, and, in the case of applications of XML, well-formed. However, like compilers, its error messages aren't always helpful unless you know what to look for.
Mike
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.