Recently, I noticed a code (or hashtag) in the address bar at the end of many sites. For example: https://site.ru/blablabla/#.V49UWuLLfK4

And with each page refresh, it changes, and the link works without it. Especially with this medium.com . What is it for and how to do it?

Just do not write about the anchors. It is not them.

    1 answer 1

    This part of the URL is called the fragment identifier . A piece of URL, extract the meaning from which the client , not the server. Therefore, when requesting a document via HTTP, the client may not even transmit this part in the request (it may even be obliged not to transmit, but you need to look at the standard). It is semantically and high level.

    At first (and already quite a long time) this part was used only for anchors in browsers. The server returns the entire document, and the client looks into the fragment, searches for the corresponding anchor and scrolls to view it solely for the convenience of the user. That you know.

    With the spread of JavaScript, access to this part was gained not only by the browser, but also by arbitrary code sent by the server. And this part was a good place to store a small piece of client data, which the server does not need to be aware of, and the client can somehow react. For what? Well, for all that JavaScript can do, you just need to apply your imagination.


    Now it is used to store state in a small SPA right inside the link. It does not have to be a SPA, it can be any browser application on JS, maybe even hosted on several pages. It's just a short term and understandable.

    Client A can send a link to client B using his own methods and, opening it, you can see the application in exactly the same state as client A. Example: Coriolis.io, a tool for exchanging ship assemblies in Elite: Dangerous .

    Another variation is not storing the entire state of the application, but only the part that may be useful to other users.

    For example, routing. URL like http://site.com/#/thing/42 It was carried out mainly through the fragment part. It has not yet been HTML5 History API . Now it is. And now when the server can be taught to understand any URL, you can write SPA, changing the "current URL" within one page without such crutches. AngularJS without "HTML5 mode" works this way. When there is no programmable server part (or it is too lazy to configure what happens), this is the only way of client routing that does not break the resulting links: the server ignores the fragment part.

    Or exchange tokens. There was no time for the dominance of messengers working through WebRTC, and rooms were formed through a fragmentary piece. The SPA looks for a room token there; if it does not find it, it generates a random one and adds it there. The user needs only to throw the link. Those who have passed through it will get to the SPA, and the JS-client in it will see the token and immediately be asked to the specified room, having gotten to the one who dropped the link.