Why Is the Hash Part of the Url Not Available on the Server Side

Why is the hash part of the URL not available on the server side?

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

How to get Url Hash (#) from server side

We had a situation where we needed to persist the URL hash across ASP.Net post backs. As the browser does not send the hash to the server by default, the only way to do it is to use some Javascript:

  1. When the form submits, grab the hash (window.location.hash) and store it in a server-side hidden input field Put this in a DIV with an id of "urlhash" so we can find it easily later.

  2. On the server you can use this value if you need to do something with it. You can even change it if you need to.

  3. On page load on the client, check the value of this this hidden field. You will want to find it by the DIV it is contained in as the auto-generated ID won't be known. Yes, you could do some trickery here with .ClientID but we found it simpler to just use the wrapper DIV as it allows all this Javascript to live in an external file and be used in a generic fashion.

  4. If the hidden input field has a valid value, set that as the URL hash (window.location.hash again) and/or perform other actions.

We used jQuery to simplify the selecting of the field, etc ... all in all it ends up being a few jQuery calls, one to save the value, and another to restore it.

Before submit:

$("form").submit(function() {
$("input", "#urlhash").val(window.location.hash);
});

On page load:

var hashVal = $("input", "#urlhash").val();
if (IsHashValid(hashVal)) {
window.location.hash = hashVal;
}

IsHashValid() can check for "undefined" or other things you don't want to handle.

Also, make sure you use $(document).ready() appropriately, of course.

Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.

Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.

Is the URL fragment identifier sent to the server?

Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.

How to get hash in a server side language?

Short answer: no, it's a client side only thing.

Slightly longer:
It's called Fragment identifier and is client side only - browser doesn't send that part of the url to the server. It's not possible in any language unless you are using some browser implementation (or software) which would send that part of url to the server. Reference: http://en.wikipedia.org/wiki/Fragment_identifier

Is the URL's hash available on the server with Node.js?

No, there will not be a hash in the URL sent from the client to the server as part of the HTTP request.

But that's not the only case where Node would need to manipulate URLs. You could well be writing server-side code to generate a URL that will be inserted into the HTML sent back to the client (e.g., you might have a list of hyperlinks, and be generating the URLs for those hyperlinks).

For that reason, it makes sense for Node's URL API to be complete, even if one part of the URL will be missing in one common use case.

Getting the full url of the current page with url hash on server side

there is no way to get hash content on server side because hash are never posted to the server

see this question for some tricks How to get Url Hash (#) from server side

In django, hashtag is not matching with the urls

There is one issue with your implementation.
1. Browser does not send # part to the server. So if your URL is like /tags/#tag then #tag won't be sent to the server. Further read: Why is the hash part of the URL not available on the server side?

Because of this behavior, your browser will hit /tags/ url. That is the reason of your 404 error.

You can check the example of twitter, If the hashtag is #DelhiElectionResults, then the url for that hashtag is https://twitter.com/hashtag/DelhiElectionResults.

Solution: Just remove # from the url and make it like: /tags/tag/. In your JS, you can use value.replace('#', '') to remove the # from the value.

$.each(arrElems, function(index, value){
strText = strText.toString().replace(value, '<a href="/tags/'+value.replace('#', '')+'">'+value+'</a>');
});


Related Topics



Leave a reply



Submit