tags:

views:

193

answers:

6

Hello!

Can someone tell me, what the href with the "#" means?

<a id="logoutLink" href="#">Logout</a>
+5  A: 

It is the shortest way to say "go nowhere" :)

Often something else is bound to that link, a javascript event handler in most cases. But if an <a> doesn't have a href, most browsers don't render it with the same styling...so you need a short something to put there.

If it had something following the hash, like <a href="#topics">Go to Topics</a> it is a scroll to link, it goes to where the element with id="topics" is at the top of the page. A common example is <a href="#top">Go to Top</a>, and you stick a <div id="top"></div> at the very top of the page.

Nick Craver
It's not valid HTML/XHTML to have an `<a>` element with no `href`.
zneak
@zneak - Fair point, I suppose I forget to mention that most times since I would never think to use an anchor if you weren't going anywhere :)
Nick Craver
+2  A: 

It means nothing... ;) Normally we use #something to create anchor to some element. If your URL ends with ...#comments then your browser will automatiacly jump (scroll the page) to element with id="comments".

href="#" is often used to create a link that leads to nowhere.

Crozin
Actually the link doesn't create the anchor, it just tells the browser to jump to that anchor when the link is processed.
poke
+1  A: 

If points to an anchor on the page. In this case the anchor is just default. You could have multiple anchors on the page

<a name="anchor1"></a> <a name="anchor2"></a> <a name="anchor3"></a>

and link to them as

<a href="#anchor1">link 1</a> <a href="#anchor2">link 2</a> <a href="#anchor3">link 3</a>

WDuffy
+1  A: 

It means make this page an anchor and navigate to it - which is why you see '#' after your URL in the address line (which can have nasty side-effects). Its also why your page will scroll back up to the top if you click on the link (sidenote: you can avoid this by adding "return false;" at the end of your onclick handler)

plodder
Can you elaborate on the side effects?
Martin Smith
for example you might be using regex to parse your URL - the extra # can throw it out
plodder
+4  A: 

As others have pointed out, hash anchors (those beginning with the # sign) typically lead to a named anchor on the page. A table of contents on a page might be a good example of where you'd see this:

<ul>
  <li><a href="#history">Company History</a></li>
  <li><a href="#goals">Our Goals</a></li>
  <li><a href="#products">Products We Offer</a></li>
  <li><a href="#services">Services We Offer</a></li>
</ul>

<h2><a name="history">History</a></h2>
<p>The #history anchor tag will lead to the named anchor above.</p>

<h2><a name="goals">Our Goals</a></h2>
<p>The #goals anchor tag will lead to the named anchor above.</p>

<h2><a name="products">Products We Offer</a></h2>
<p>The #products anchor tag will lead to the named anchor above.</p>

<h2><a name="services">Services We Offer</a></h2>
<p>The #services anchor tag will lead to the named anchor above.</p>

One thing to note is that when you have a blank hash as the anchor href (i.e.: <a href="#">Blah</a>), some browsers make that jump to the top of the page, which is not the desired effect. To work around this and prevent the page from scrolling all the way to the top, a JavaScript implementation is often included to prevent the anchor tag from acting as it normally would by returning false.

<a href="#" onclick="return false;">Blah</a>
Matt Huggins
You should just be able to do <h2 id="history">History</h2> these days.
robertc
@robertc - Do you have a link that demonstrates that's the expected implementation from the W3C or anything like that? I'm not discounting your statement, I just like to keep up to date about this kind of stuff and can't find anything official that states the same.
Matt Huggins
@robertc - Nevermind, I found a statement from here: http://www.w3.org/TR/REC-html40/struct/links.htmlIt states: "Destination anchors in HTML documents may be specified either by the `A` element (naming it with the `name` attribute), or by any other element (naming with the `id` attribute)."Thanks for pointing that out!
Matt Huggins
I think the main reason why it's not more well known is that a lot of browsers didn't support it at the time when the standard came out.
robertc
A: 

The hash symbol (i.e., <a id="logoutLink" href="#">Logout</a>) is a placeholder, so that you can preview the "Logout" link in a browser as you develop your page. Eventually, the hash symbol will be replaced with a real URL.

seasonedgeek