0

Here is the HTML code I want to write in the output page:

Response.Write ("<a href=\"page2.aspx\">Page2" + 
                "<a href=\"page3.aspx\">Page3</a></a>");

I expect the following output:

<a href="page2.aspx">
  Page2
  <a href="page3.aspx">
    Page3
  </a>
</a>

But I get the following one:

<a href="page2.aspx">
  Page2
</a>
<a href="page3.aspx">
  Page3    
</a>

Does anyone know why Response.Write closes the tag and how to change this behaviour?

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
Nicolas
  • 6,289
  • 4
  • 36
  • 51

2 Answers2

3

HTML forbids nested anchors.

Content model:
Transparent, but there must be no interactive content descendant.

The a element

Either ASP is cleaning up your markup, or you are looking at in a DOM inspector after the browser has tried to recover from your error.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Exactly, I would like to know if ASP modify my page or if it's my browser which correct this forbidden nested anchor. When I write the expect HTML code myself, firefox displays it correctly. – Nicolas Oct 25 '13 at 09:33
  • Easy enough to tell: Use View Source instead of a DOM inspector. And firefox doesn't display it "correctly", there is no "correctly". Stop trying to write invalid HTML. – Quentin Oct 25 '13 at 09:34
  • Nice, View Source confirms that ASP doesn't modify my page. It's my web browser which does it. It's all the firefox fault, which respects the HTML rules. Thanks for your explanation. – Nicolas Oct 25 '13 at 09:42
2

You cannot nest a element inside another a, HTML doesn't allow that, it's invalid, browser will just parse them as separate a tags.

As Per W3C1

1 Links and anchors defined by the A element must not be nested; an A element must not contain any other A elements.


If you are looking to open multiple links on a single anchor tag, consider using onclick event with window.open(), refer my answer here for more details.

Community
  • 1
  • 1
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • Ok, I see, nested anchors are forbidden in good practices. But it works if I write the HTML myself (firefox manage it). So my question is why the `Response.Write` method modify my string (may be, just because it's forbidden to nest anchors :) ). Thanks for the `onclick` it's agood idea. – Nicolas Oct 25 '13 at 09:29