In general, can we consider an HTML document that is initially invalid but eventually becomes technically valid (through scripting) to be OK?
"OK" would be per good practices, or possibly with regard to the standard, if it answers this question.
For example, the document that results from parsing this serialized HTML markup validates initially using the W3 validator:
<!DOCTYPE html>
<title>foo</title>
bar
While this one doesn't:
<!DOCTYPE html>
<script>document.title = 'foo'</script>
bar
And that is even though the result is exactly the same for any browser that supports Javascript. Assuming this is a web application and JS is required, is this kind of thing "OK"?
I especially wonder about this situation when we don't have any correct (from an application perspective) way of satisfying the standard initially. For example, what if we don't know the title of the document initially, and must compute/retrieve it using a script?
In this particular case, using a placeholder feels wrong:
<!DOCTYPE html>
<title>placeholder</title>
<script>document.title = 'foo'</script>
bar
(Note that leaving the title element empty is still considered invalid.)
So, without debating too much about the title element in particular, is it generally accepted to distribute HTML resources that are only eventually valid?
Subquestion: I realize that validating the document (as represented by the DOM) and validating its serialized markup are two different things; are there any tools to do the former? (Either from a snapshot of the DOM or "continuously".) Example:
<!DOCTYPE html>
<title>foo</title>
<script>document.title = ''</script>
bar
This validates initially but technically results in an invalid document, without any obvious way to detect it.
Update: Obviously such a tool would have limited value in a static analysis context (halting problem, etc). A runtime tool should be useful however.
Update: W3C spec for DOM validation (Level 3).
Update: W3C spec for Service Workers, which seems like it could be used to ensure a DOM is valid before being rendered, even if the template is not (avoiding the use of placeholder elements and such). It's still early at the time of this writing (June 26th 2014, so don't quote me on that).