0

In my django project i have a url:

https://example.com/nice_page#123

This url will lead user to particular post on my page, this is an example how it works on stackoverflow:

What is the "N+1 selects problem" in ORM (Object-Relational Mapping)? (it is a link to particular answer, and your browser will move you right to this answer.)

My goal is to get this #123 id from url.

I can't do it with django's META data by request.META it doesn't show this #123 id, it only returns me https://example.com/nice_page

How can i get this #123 id? I would prefer making it with django, but javascript is also acceptable.

Update: as follows from the comments, thanks to – Willem Van Onsem and Ankit Tiwari, this task is unsolvable by server-side (by django) How to access url hash/fragment from a Django Request object

oruchkin
  • 1,145
  • 1
  • 10
  • 21
  • 2
    The *fragment* (part after the `#`) is *never* send to the server... – Willem Van Onsem Aug 24 '22 at 09:14
  • @WillemVanOnsem, but can i get done it with javascript? How did stackoverflow did it? They got this shiny glowing on their posts which you share – oruchkin Aug 24 '22 at 09:17
  • 1
    Does this answer your question? [How to access url hash/fragment from a Django Request object](https://stackoverflow.com/questions/2181186/how-to-access-url-hash-fragment-from-a-django-request-object) – Ankit Tiwari Aug 24 '22 at 09:18
  • @AnkitTiwari your answer says, it unsolveable by server-side, but i still have client-side, i can do it some how with javascript – oruchkin Aug 24 '22 at 09:20
  • 1
    @oruchkin: that is just some css... Nothing at the server side... See the `:target` selector https://www.w3schools.com/cssref/sel_target.asp – Willem Van Onsem Aug 24 '22 at 09:50

2 Answers2

2

Yes, only with JavaScript. E.g. using an example from here:

const url = new URL('https://example.com/nice_page#123');
console.log(url.hash); // Logs: '#123'

Or reference the hash of the current URL in a browser:

console.log(window.location.hash);
Peter
  • 131
  • 6
  • this is almost what i need, but how do i get `URL` this one is only example, but in reality it is dynamic – oruchkin Aug 24 '22 at 09:28
  • Ah, you could use `window.location.hash` in a browser to get the hash of the current page? https://developer.mozilla.org/en-US/docs/Web/API/Location/hash – Peter Aug 24 '22 at 09:36
  • yes it is, it is solved by only one line of code, marvelous! `console.log(window.location.hash)` it returns `#123` update your answer on this solution, i'll accept you answer – oruchkin Aug 24 '22 at 09:40
  • 1
    Sure yeah, added that, too. – Peter Aug 24 '22 at 09:43
0

You could use location.search and URLSearchParams.

https://example.com/nice_page?id=123

const params = new URLSearchParams(window.location.search);
const id = parseInt(params.get("id"));
console.log(id);

You can read more about here: https://developer.mozilla.org/en-US/docs/Web/API/Location/search

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
MaxCode
  • 119
  • 6
  • `params` variable returns an object, yes, but it has no `id`, so in the end it returns NaN – oruchkin Aug 24 '22 at 09:33
  • ahh that is weird I tried in my computer with this URL http://127.0.0.1:5500/nice_page.html?id=123 and it works. – MaxCode Aug 24 '22 at 09:38
  • 1
    i don't have id after question mark, i have it after hash `https://example.com/nice_page#123` but thanks for helping! – oruchkin Aug 24 '22 at 09:41