<noscript> in <head> - html

Am I allowed to place <noscript> in the <head>?

According to the XHTML Strict DTD, no, you're allowed script, style, meta, link, object, title and base only. Transitional allows isindex as well, but still not noscript.

Using the HTML5 Doctype, I have a declaration in my <head> with a <link> element that points to a no-js.css stylesheet. It validates and seems to work fine.

You are as long as you do it in HTML documents and not XHTML.
In a head element [...], the noscript element must contain only link,
style, and meta elements.
See specification.

HTML5 adds support to <noscript> in the <head>: https://www.w3schools.com/tags/tag_noscript.asp

Related

What should a basic html5 file include?

I was wondering what an html file should ALWAYS include besides <html>, <header> and <body>. I've seen many things, so I'm not sure what to include ALWAYS.
The html, header and body tags are actually optional in HTML5. The only required element, aside from the doctype definition, is title. So the following would be a completely valid HTML5 document:
<!DOCTYPE html>
<title>example</title>
You can validate that using the W3C Validator.
Actually the start/end tags of the html/body tags are optional IF:
The <html> start tag is optional unless the first thing inside the html element is not a comment.
The <⁄html> end tag is optional unless the html element is not immediately followed by a comment.
see html tag, optional vs. required
and the body start/end tags may be omitted IF : see when

Empty <title> element in HTML stops HTML from loading if written as <title />

Probably a really dumb question - but in an HTML document, why does the following:
<title></title>
work ok (according to http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_basic)
but then
<title />
will stop the HTML from rendering?
Thanks
As stated in the HTML specification:
The title element must not be empty.
It also states:
Note: If it's reasonable for the Document to have no title, then the title element is probably not required.
The <title> tag must have a start and an end tag, it's required:
Tag omission
A title element must have both a start tag and an end tag.
In an HTML document delivered as text/html, as web pages normally are, the tag <title /> is processed by browsers as if the slash were not there. That is, it is just the start tag. And the content of title is parsed as text – character references/entities are recognized but no markup, except the element’s own end tag. So all the rest will be taken as the content of the title element, leaving no visible content to render.
Formally, <title /> is invalid in all forms of HTML up to and including HTML 4.01. In HTML5, it is permitted, but in HTML serialization, it means just <title>.
In genuine XHTML, i.e. in XHTML when delivered with an XML media type (rare on the web), <title /> means the same as <title></title>. This is valid except according to HTML5 drafts, which require nonempty content here. But it is not recommended, because it does not work that way when a browser, or other program, processes the document as if it were in HTML syntax.
Cf. to Are (non-void) self-closing tags valid in HTML5?

What's a valid HTML5 document?

I've just been reading the HTML5 author spec.
It states that the <html>, <head> and <body> tags are optional.
Does that mean that you can leave them out completely and still have a valid HTML5 document?
If I'm interpreting this correctly, it means this should be completely valid:
<!DOCTYPE html>
<p>Hello!</p>
Is this correct?
You can check out the spec here:
http://dev.w3.org/html5/spec-author-view/syntax.html#syntax
"8.1.2.4 Optional tags" is the bit out about it being OK to omit <html>, <head> and <body>
The title element is indeed required, but as Jukka Korpela notes, it also must be non-empty. Furthermore, the content model of the title element is:
Text that is not inter-element whitespace.
Therefore, having just a space character in the title element is not considered valid HTML. You can check this in W3C validator.
So, an example of a minimal and valid HTML5 document is the following:
<!doctype html><title>a</title>
This is the minimal HTML5-valid document:
<!doctype html><title> </title>
W3C HTML validator maintainer here. FYI with regard to the validator behavior, as of today, the validator now enforces the requirement in the HTML spec that the title element must contain at least one non-whitespace character -
http://validator.w3.org/nu/?doc=data%3Atext%2Fhtml%3Bcharset%3Dutf-8%2C%3C%2521doctype%2520html%3E%3Ctitle%3E%2520%2520%2520%3C%252Ftitle%3E
While the <html>, <head> and <body> start and end tags are optional, the <title> tags are required, except in special circumstances, so no, your sample is not (ordinarily) valid.

How to use the <noscript> element "right"?

The HTML validator I found today - http://html5.validator.nu/ - says that my use of the <noscript> element is wrong. My XHTML source code is like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<body>
<div id="head">
<noscript>
<p>JavaScript is disabled.</p>
</noscript>
...
The error message from the validator is this:
Error: XHTML element noscript not allowed as child of XHTML element div in this context. (Suppressing further errors from this subtree.)
Contexts in which element noscript may be used:
In a head element of an HTML document, if there are no ancestor noscript elements.
Where phrasing content is expected in HTML documents, if there are no ancestor noscript elements.
Content model for element div:
Flow content.
Now I went on to the Mozilla Documentation and tried to understand what that means. I have found some information about content categories, flow content, phrasing content, what elements belong to each category (whatever "belong" means exactly) and how the <noscript> element may be used. (https://developer.mozilla.org/en/HTML/Element/noscript)
I now know this: <div> must contain flow content. <noscript> must occur in phrasing content. That obviously doesn't match. How can I manage that? Many elements are in both, flow and phrasing, categories though. They don't seem to be disjunct sets, so some can't decide or I don't get it.
How does the HTML specification intend to solve this quirks?
You shouldn’t use an HTML5 validator to validate your XHTML 1.1 document.
The HTML5 spec says about the noscript element:
The noscript element must not be used in XML documents.
So noscript inside div isn’t allowed in XHTML5, but it’s allowed in XHTML 1.1.
You can't use "noscript" in an XHTML/XML document because it disables the parser for the "noscript" content and you can't do that in XML. The parser cannot be temporarily disabled or turned off.
For example, CSE HTML Validator generates this message for XHTML documents:
XHTML documents must not use the "noscript" element. This is because the way "noscript" works by "turning off" the parser when scripts are enabled, which can't be done in XML. Also, the "noscript" element is explicity not allowed in HTML5 for this reason.
If you want to use "noscript" properly, then you'll need to change the document type to HTML5 (recommended) or HTML4 instead of XHTML.
Use the official validator to validate your HTML. The following code validated perfectly fine for me:
<!DOCTYPE html>
<html>
<head>
<title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
</head>
<body>
<div id="head">
<noscript>
<p>JavaScript is disabled.</p>
</noscript>
</div>
</body>
</html>
EDIT: Interestingly, the W3C validator claims that the parser is based on the validator at the link you provided.
'The uploaded document was successfully checked as HTML5. This means that the resource in question identified itself as "HTML5" and that we successfully performed a formal validation of it. The parser implementations we used for this check are based on validator.nu (HTML5).'
Perhaps the validator at http://validator.nu is an older version. Edit edit: Nope, it validates just fine there too. Checked as HTML5 and XHTML 1.0 Strict.

Why won't <iframe> elements validate in HTML 4.01?

I was just checking to see if it was valid to put an <iframe> element inside a <noscript> element as a fall back for displaying dynamic content. It validated fine with the HTML 5 doctype, but for HTML 4.01, I get the following error:
Line 9, Column 35: element "IFRAME" undefined
<iframe name="test" src="test.htm"></iframe>
You have used the element named above in your document, but the document type you are using does not define an element of that name. This error is often caused by:
incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Frameset" document type to get the "" element),
by using vendor proprietary extensions such as "" or "" (this is usually fixed by using CSS to achieve the desired effect instead).
by using upper-case tags in XHTML (in XHTML attributes and elements must be all lower-case).
This is what I whittled the HTML down to:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<iframe name="test" src="test.htm"></iframe>
</div>
</body>
</html>
The <iframe> element is defined in the HTML 4.01 specification at the following URL: http://www.w3.org/TR/html401/present/frames.html#h-16.5.
It passes with a transitional doctype, so I guess my question is "Why is it disallowed in a strict doctype, even though it's defined in the specification?".
"Why is it disallowed in a strict doctype, even though it's defined in the specification?
Lots of things are defined in the specification but not allowed in Strict. <font> springs to mind. These are the things that the developers of the specification considered in need of documenting, were in use in browsers in the day, but which should be transitioned away from.
I can think of two reasons why they might have thought that:
"Why do iframes suck?".
<iframe> does (in theory) little that can't be achieved with <object>
iframe isn't included in html strict. For validation, try using the object element instead.
<object data="test.html" type="text/html"></object>
You should also add width and height attributes to the object element. Note, unlike iframes objects cannot be a target for any page links.
Unless for some reason you specifically need html4 strict validation, it's better to use the html5 doctype.