views:

63

answers:

2

I have a question about the href link, tried googling it but could not find much info on this. I have a href link like this:

<a href='#' onclick='openSerialWindow();return false;'><h:outputText value="#{i18n.regFindSerialNumber}" /></a>

previously the # was replaced by the page.htm that it should link to and that caused an error when user right clicked on the link and chose 'Open in new window/tab'. After replacing the page.htm with # it works fine user can even r/c and open it in new tab/window.

If user simply clicked on the link both ways above worked (the # and page.htm), so I am wondering what is the true meaning of #?

thanks.

+1  A: 

# is an anchor hash and points to the top of the current page.

You can create anchors in your document like this: <a name='anchor'></a> and then jump to them by adding #anchor to the page's URL. The browser will jump to the position without reloading.

If linking to page.htm produced an error, then page.htm is an incorrect link.

The href attribute is there only as fallback in case there is no Javascript. Javascripts's return false; prevents execution of the link. Linking to # means that if javascript is turned off, nothing will happen by clicking on the link except that the browser will jump to the top of the page.

Pekka
Furthermore unless something **REQUIRES** javascript to work (for instance an ajax link) you should always use the valid URL to the intended page as the `href` and the jsut return false from your onclick - that way he page degrades for those who dont have JS for some reason.
prodigitalson
Linking to page.htm ONLY gives a 404 error when user right clicks and choses 'Open in New tab/window', if the link is simply clicked there is no error, it works just fine. whereas with the # it works both ways.
msharma
A: 

See: http://www.w3.org/TR/html4/struct/links.html

A # indicates a link to a [named] anchor within a page.

An <A> which invokes some javascript generally doesn't work with the "open in new window/tab" command.

Seth