Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting hash parameters from request url

Tags:

javascript

I have such url - http://www.coolsite.com/daily-plan/#id=1 What the easiest way to parse that string and read a hash value (the value after #id=)? Thank you

like image 394
nKognito Avatar asked Nov 07 '11 06:11

nKognito


People also ask

What is hash parameter in URL?

Hash parameter authentication. You can authenticate individual REST requests by adding a hash parameter to the URL. The hash parameter allows you to prepare REST requests that can be executed by unauthenticated users. Requests that contain the hash parameter ignore the credentials specified in the authentication header ...

Can URL contain hash?

In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. When you use a URL with a # , it doesn't always go to the correct part of the page or website.


1 Answers

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.

like image 109
Insomniac Avatar answered Sep 22 '22 06:09

Insomniac