tags:

views:

107

answers:

4

I have inspected some sites and they have a pound(#) sign in the url. What does it do?

 <a href="#" >Link name</a>
+1  A: 

This links back to the page itself. It's often used with links which actually run some JavaScript.

Kinopiko
+11  A: 

It's a "fragment" or "named anchor". You can you use to link to part of a document. Typically when you link to a page, the browser opens it up at the top of the page. But you link to a section half-way down, you can use the fragment to link to that heading (or whatever).

If there is no <a name="whatever"/> tag within the page, then the browser will just link to the top of the page. If the fragment is empty, then it will also just link to the top of the page.

For a fragment only <a href="#">Link name</a>, then that's just a link to the top of the current page.

You often see that kind of link used in conjuction with javascript. Standards compliant HTML requires a href attribute, but if you're planning to handle the request with javascript then "#" serves as a reasonable place holder.

Dean Harding
+1 Although the official term is a URL fragment, not a "hash reference": http://www.w3.org/TR/WD-html40-970708/htmlweb.html#h-4.1.1
Jason Hall
Yeah, I changed it to "fragment" in my answer, cause that's what most people use. I never knew it had an "official" name though :)
Dean Harding
Why browsers treat it as 'go to top of page': Technically, you changed the page. After clicking the link, you'll notice that the # is actually added to the URL in the address bar, and if you click the back button it will remove it again. I wouldn't say it's a link to part of a document, more a link to a place inside the document. Otherwise, basically the same thing I was typing...
animuson
+3  A: 

... just to add a few extra useful tips.

You can access and change it with document.location.hash in JavaScript.

It can point to a named anchor (e.g. <a name="top"></a>) or to an element with a corresponding id (e.g. <div id="top"></div>).

Seeing one on its own (e.g. <a href="#" onclick="pop()">popup</a>) generally means a link is being used to run JavaScript exclusively. This is bad practice.

If your JavaScript'd link can not actually point to another document, consider using a button element or similar.

alex
That's a good point about it not particularly good practise... but just because it's not good doesn't mean you won't find it being used like that all over the place :)
Dean Harding
A: 

# indicates a link to an anchor.

I thougt I'd also mention something else:

Using '#' as the href for a link that activates JavaScript is bad because it scrolls the page to the top - which is probably not what you want. Instead, use javascript:void(0).

George Edison