48

For example if I type in the URL:

http://www.foo.com/page.php?parameter=kickme#MOREURL

Then on the server there is no part: #MOREURL

Is possible to send or get these part to the server without jQuery AJAX?.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
CRISHK Corporation
  • 2,948
  • 6
  • 37
  • 52

4 Answers4

68

No, it is available to the browser only, so you have to deal it with Javascript. The server can not read it.

Explanation:
Basically the hash component of the page URL (the part following the # sign) is processed by the browser only - the browser never passes it to the server. This sadly is part of the HTML standard and is the same whether or not you are using IE or any other browser (and for that matter PHP or any other server side technology).

Here's what Wikipedia says about it:

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the fragment value. In the most common case, the agent scrolls a Web page down to the anchor element which has an attribute string equal to the fragment value. Other client behaviors are possible

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • 1
    Well, actually you could `urlencode('#')` the hash to"%23" and then `urldecode('%23')` to "#" again on the server, so it *does* get transferred to the server. This does not use JavaScript, as required by OP, but PHP. If you also don't want to use PHP, you can write "%23" in your HTML code by hand ;-) –  Apr 10 '15 at 12:56
  • 3
    No @what, that is not reliable. If the hash were after a query string, the `%23` would simply be treated as part of the value ie `?foo=bar%23what`, would cause the server to literally interpret as $foo = 'bar#what'. – Oliver Williams Feb 02 '17 at 04:28
  • @OliverWilliams ya better approach is to use query params – sktguha Aug 11 '17 at 13:49
  • I finally inferred this browser behavior after searching the web for a long time trying to understand why PHP cannot read the full URL entered by the user or in the link that was clicked. So frustrating that this is not better documented on the Web. This is one of the rare answers that is correct. It means that PHP cannot redirect or do anything else with incoming URLs containing anchor names--what a poor design! – David Spector Jan 02 '20 at 17:41
  • Great answer! I had been trying to find why the part in the URL after `#` is not being sent to the server. – Binita Bharati Jan 21 '21 at 05:46
10

https://www.rfc-editor.org/rfc/rfc2396#section-4

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

Community
  • 1
  • 1
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • In conclusion, #MOREURL is not sent to the server? Only using jQuery/AJAX/Javascript methods. – CRISHK Corporation Sep 08 '10 at 02:38
  • jQuery and AJAX have nothing to do with this. It's only accessible via Javascript or possibly another client side scripting language. Not server side. – meder omuraliev Sep 08 '10 at 02:39
  • 1
    @user439866: A hash is almost like an extension number in a phone system. It's "receiver side." You dial the phone number and then the extension - but the extension isn't actually part of the phone number (unless its some sort of direct dial system) - the extension is an "extra" part that is processed separately by the receiving phone system. I actually don't know much about how phone systems work, so this analogy may be incorrect. – Cristian Sanchez Sep 08 '10 at 02:50
8

I would like to extend the answer on the reason WHY the fragment is not sent to the server. Because it is intentional and desired behavior. Let's look at URL string in whole.

/path/to/element?query=string&for=server#?optional=fragment&for=browser <----- URI ----> <---- QUERY STRING ---> <----- FRAGMENT STRING ------>

URI uniquely specifies resource fetched from a server

QUERY defines operations to be performed by the server on the resource

FRAGMENT controls browser (application) behavior. Fragment should be used to store application state which should be visible to the user so the user can send link to another user to get the same application state.

Fragment is the only part of URL free for you to transparently implement single-page web applications (which can run offline on your mobile phone for example). Therefore it must not be sent to the server.

katomaso
  • 400
  • 3
  • 10
  • It may work, but that doesn't mean it works entirely as desired. If a URL can be written with an anchor name, that should be an official part of the URL and sent to servers with requests. A PHP script cannot redirect a URL that contains an anchor name because the anchor name is never received. – David Spector Jan 02 '20 at 17:45
  • Yep, that's the only sad part about this standard, there is one really valid use case and that is redirecting a section of a documentation for example to another page, if you use hash to go to that section, you need to redirect with js – Tofandel Oct 13 '22 at 06:59
0

The hash component is not passed on to the server but it is extensively used on the client side. Specifically, in single page applications, the text following a hash is used to represent state of the application as different routes. Thus, what happens is: following an initial request to the server which serves the 'home' page along with additional js files which include client-side routing logic such as the router, whenever the user navigates anywhere on the page by clicking an anchor tag, only the part of the URL following the hash component is changed. This prevents a GET request to the server and in response to this 'onhashchange' event, the content of the single page application can be updated depending on the exact route.

Mudit Jha
  • 57
  • 2
  • 7
  • In addition to the client-side processing, a full URL should be sent to the server in case the server needs to redirect it, or record it. – David Spector Jan 02 '20 at 17:53