-1

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?

mauguerra
  • 3,728
  • 5
  • 31
  • 37

4 Answers4

2

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.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
2
  1. 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.

  2. 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)

  3. in a url like http://domain.com/page2.htm#page1.htm you can easily know where do you came from using document.location.hash

  4. 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 the anchor 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 return false 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.

balexandre
  • 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
1

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.)

Bruno
  • 119,590
  • 31
  • 270
  • 376
1

# 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.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Tats_innit
  • 33,991
  • 10
  • 71
  • 77