Getting hash parameters from request url

On client side (i.e. from JavaScript) you can check window.location.hash to get hash. On server side, general answer is ‘it is impossible’ since hash is not sent in request to server.

Upd: I maybe misunderstood the question. My answer is about how to get hash part of url either in browser or in server side code during request processing, not about string processing.

Upd2: Answer to comment here because it doesn’t fit in comment.

How does it work when user clicks on your navigational links?

I assume hash is changed and corresponding content is downloaded via AJAX request from web service or REST.

For example if your user has URL www.example.com in his browser and this page shows a list of product categories. User clicks one category and URL changes to www.example.com/#id=5 and products from that category(with ID=5) are downloaded via AJAX and shown on the page. No postback, only partial page refresh.

Is this close to your scenario?

Now you want user to paste/enter www.example.com/#id=5 directly in the browser address bar and go directly to list of products in that category.

But /#id=5 is not sent to server with request by the browser, so there is no way to get that value on server side, and you can do nothing about it since it is the browser decided not to send this data and you don’t have it on server side.

In our project we use solution when server returns only common page code/html, i.e. header, footer, without main/center part of the page. Then there is a JavaScript code which executes right after this common HTML loaded. It takes window.location.hash and sends it to web service via AJAX and web service returns content (HTML) for the main part of the page.

Leave a Comment