I am working with JavaScript and AJAX. One of the AJAX function adds the "#" sign at the end of the URL. The function works, but I want to know why only this function adds that sign and not the other functions. Could the "#" sign bring some problems on the future?
-
Did you write the ajax functions yourself? or from a library? please be more specific – calebds Feb 15 '12 at 19:22
-
Are you using a JS framework? We can't say for sure unless we know which functions you are using. – Jeff Hines Feb 15 '12 at 19:23
-
Hint: [...15/sign-on-url#footer](http://stackoverflow.com/questions/9299815/sign-on-url#footer) :P – Andrew Feb 15 '12 at 19:23
-
@paislee i wrote the functions by myself – mauguerra Feb 15 '12 at 19:27
-
@mauguerra then simply return false! – balexandre Feb 15 '12 at 19:31
-
1You should use `e.PreventDefault()` instead of `return false` to avoid disrupting the event distribution (unless you *intentionally* want that, but that shouldn't be your default reaction). – Kitsune Feb 15 '12 at 19:34
4 Answers
It's mostly likely not the javascript that is adding the #
but rather the link you are clicking to trigger the javascript.
Many people will do something like this:
<a href="#" onclick="something()">text</a>
This way the text will appear like a link, but clicking on the link will not take the user away from the page and will execute some javascript. The side effect of this is that #
is appended to the URL and the user is scrolled to the top of the screen.

- 77,516
- 14
- 110
- 130
a
#
sign on a URL is called the hash sign and tend to separate the URL from the anchor on the page, so you can navigate to portions of the page and not only to the top of the page.In the AJAX world, this can be also used to keep the current page and you can easily know where you came from (as pages are loaded async) so you can easily go back without using the
history
api (alpha version of jQueryMobile used this technique)in a url like
http://domain.com/page2.htm#page1.htm
you can easily know where do you came from usingdocument.location.hash
but normally is simple having an anchor tag with the hash as
href
like<a href="#">click here</a>
witch normaly is a simple trick to let theanchor
tag know to behave like a link but go nowhere ... and if you don't want that to be written in your URL, you simply have to returnfalse
so the anchor link do not jump to#
(witch is the current page).
BTW, this will not, in any way, mess up with your code or any future code, it's just bad practice.

- 73,608
- 45
- 233
- 342
-
1+1 A note on #4: In jQuery, returning false will prevent the event from bubbling, too. To continue bubbling but not navigate to the URL in the href attribute, you can use [event.preventDefault()](http://api.jquery.com/event.preventDefault/) instead. [This question](http://stackoverflow.com/questions/1357118/javascript-event-preventdefault-vs-return-false) is a nice reference. – grossvogel Feb 15 '12 at 19:45
-
1@grossvogel I didn't want to say anything that was about javascript frameworks as plenty use **mootols** or any other than **jQuery** and what you say is only applied to jQuery. Each framework as it's own implementation of returning false... – balexandre Feb 15 '12 at 21:09
What's after the hash sign (#
) on the URL is called the fragment.
It was initially intended (and still used) for local anchors in HTML documents.
However, it's also typically used by AJAX applications to be able to maintain a history state, so as to enable the use of the back-button in the browser.
For example the jQuery BBQ plugin allows you to read/set parameters (similarly to query parameters, the ones after ?
in the URL) and to monitor changes in those parameters, so as to trigger other actions without reloading the full page.
In addition, for an <a />
link to be recognised as a :link
(in CSS notations), it must have a non-empty href
attribute. Typically, href="#"
is used because the URIs with and without it at the end are equivalent. (Whatever is after #
, including the hash itself isn't sent to the server as part of the request for the page.)

- 119,590
- 31
- 270
- 376
#
is used to specify the browser to center the view on a particular named element in the page.
For example: "#" sign on URL
Also, anything after #
will not pass to the server through GET parameters.

- 8,406
- 10
- 51
- 76

- 33,991
- 10
- 71
- 77