Jump to content

Can I use <a name="top" /> ?


Guest jason.sun

Recommended Posts

Guest jason.sun

Can I use <a name="top" /> instead of <a name="top" > </a> ?I noticed that Firefox, Chrome and Safari can interpret it properly while IE8 cannot. So is it the right way that follows the HTML standards? The online validation report no complain on <a name="top" />.Thanks!

Link to comment
Share on other sites

well, it depends on what kind of DTD (doctype) you are using. Since you got no complaints from the validator, you must be using XHTML. The question is, what kind of problem are you having with IE8 that the others aren't?

Link to comment
Share on other sites

Look again at the browsers where this seems to work. I tried Firefox and Safari. Specifically, I created an XHTML document with this code for the body:

<body>   <p><a name="hi" /></p></body>

When the page loads, type this in your URL box and hit the enter/return key:javascript:alert(document.body.innerHTML)Here's what the alert reported to me in both browsers:

<p><a name="hi"></a></p><a name="hi"> </a>
Look at what has happened to the <a /> element. Not quite what was expected. The code is rewritten so it looks correct, but then this extra <a> element appears out of nowhere. Browsers are supposed to interpret malformed (X)HTML if it is possible. So the page loads. But when you look at the way it's been interpreted, it's a mess.You can run a similar experiment using the Firefox DOM Inspector.
Link to comment
Share on other sites

The HTML and XHTML DTD have information explaining which tags can be empty and which can't. Only certain tags are allowed to be empty.This is the HTML DTD:http://www.w3.org/TR/html401/sgml/dtd.htmlThis is the XHTML DTD:http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_XHTML-1.0-StrictIn the HTML DTD, each element appears something like this:<!ELEMENT elementName - - EMPTY >An element that has "EMPTY" written there is allowed to be empty.If any of the two dashes are an "O" instead, the start or end tag can be omitted.In the XHTML DTD the elements would appear like this because no tags can be omitted in XML:<!ELEMENT elementName EMPTY>Some examples for the HTML DTD:The element can be empty:

<!ELEMENT IMG - O EMPTY ><!ELEMENT LINK - O EMPTY >

Both start and end tag can be omitted (the existence of the element is implied and the browser adds it automatically)

<!ELEMENT BODY O O  ><!ELEMENT TBODY O O  >

The end tag can be omitted:

<!ELEMENT P - O  ><!ELEMENT LI - O >

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...