Richard D. Worth wrote:

On Thu, Sep 24, 2009 at 12:53 PM, Bertilo Wennergren <berti...@gmail.com>wrote:

Richard D. Worth wrote:

 Best practice, valid HTML, and compatible with XHTML would be
$('#whatever').html('<div></div>');
$('#whatever').html('<br />');
$('#whatever').html('<img alt="" src="..." />');

Well, not quite "valid" HTML...

quite valid. See in the below paragraph "That means that '<br />' is valid"

Yes, valid, but valid code for "<br>>", not for "<br>".
Splitting hairs, I know...

If you use "<br />" in an HTML 4.01 page, the validator at
"w3.org" gives a warning with the following explanation:

 The sequence <FOO /> can be interpreted in at least two different
 ways, depending on the DOCTYPE of the document. For HTML 4.01 Strict,
 the '/' terminates the tag <FOO (with an implied '>'). However, since
 many browsers don't interpret it this way, even in the presence of an
 HTML 4.01 Strict DOCTYPE, it is best to avoid it completely in pure
 HTML documents and reserve its use solely for those written in XHTML.

That means that "<br />" is valid, but it does not mean what
it was supposed to mean (in HTML 4.01). It means "<br>>"!

Having to ignore this warning is a pain, and that's remedied by HTML5. As I
posted previously, in HTML5 it is allowed (not recommended nor required)
because it's in such common use for compatibility with XHTML.

Quite welcome.

Still, it's not likely that any browser will actually
produce "<br>>" "from $('#whatever').html('<br />')".
But we're talking "best practices" here.

One best practice I have is that any (X)HTML I write should require as
little modification as possible to be made one or the other. The HTML5 spec
and FAQ give me confidence that no useful browser or html parser will do
"the wrong thing" with <br /> parsed as text/html,
so it's worth the
potential ease of migration to/from XHTML. If nothing else, it's a best
practice that allows one to read and write HTML and XHTML without having to
remember as much.

Indeed.

The question about "<br />" arose when I was demonstrating
how the same inline JS example is supposed to look like in HTML
and in XHTML. When I was changing one version from it's XHTML
incarnation into an HTML incarnation, removing all the
ending slashes from "meta" elements etc., I suddenly stopped
at the included jQuery code that happend to have a piece
of '.html("<br /> whatever...")' in it, suddenly not knowing
if I'm supposed to remove that slash or not. I really did not
know - since I'm quite new to jQuery (being in the process of
converting from Prototype...). So I had to ask. Sorry about
all the hairs being split in the process!

--
Bertilo Wennergren <http://bertilow.com>

Reply via email to