Tuesday, February 27, 2007

The Anchor Tag

I used to think that the only way to make internal links inside a web page pointing to sections found in the same web page was to surround the target section with an anchor tag like this:
<a href="section-name">Some Link Text</a>
I just by coincidence while scanning the W3C documentation for anchor tags discovered that there is another method which in my case proved to be easier and more compact.
Let's say you have a section in your web page titled as follows:
<h2>My Section</h2>
If I wanted to link to such a section, I would modify it as follows:
<h2><a name="section-name">My Section</a>
But I discovered that I can alternatively write it as follows:
<h2 id="section-name">My Section</h2>
This has the exact same effect as the first method and looks neater and easier to write.

By the way, using internal anchors and linking to them from the same web page helps a lot in search engine optimization (SEO). Google seems to love web pages that have such links in them. They give spiders many clues and Google just loves this and usually ranks such web pages high.