1

I try to set the slug of an entry to a url using a hash.

https://url.spec.whatwg.org/#dom-url-hash

However it seems not possible by default, craftcms changes the hash to a dash.

I need that as fragment param for my javascript.

I have searched and read the docs but cannot find any hint to change this behaviour

1 Answer 1

2

This is impossible (not only in Craft, but in general) because the fragment (hash, the part after the #) is only used on the client-side. This is by design, it's part of the spec that the fragment is never sent to the server. The server never knows about the fragment, so it can't do routing based on it.

Using a fragment for routing is only possible if you have some sort of single-page-application which does routing in the frontend. In this case, the routing is done with JavaScript which does have access to the fragment, so it can route to a particular component based on it. Doing that on the backend (server-side) is impossible.

7
  • Indeed, but this has nothing to do with my problem... Added hint that I use the param via javascript Commented Nov 15, 2021 at 12:00
  • 1
    @HonsaStunna Please expand on what you're trying to do. If you simply want to include a fragment in an URL somewhere on your site, you have to do that in your template where you output the link, not in the entry.
    – MoritzLost
    Commented Nov 15, 2021 at 13:23
  • I need that url to go to the anchor depending on the object delivered from the craft db. Just as standard location hash developer.mozilla.org/en-US/docs/Web/API/Location/hash Commented Nov 15, 2021 at 13:37
  • 1
    @HonsaStunna Still curious about your use-case. If you describe your intended structure in more detail, I'm sure I can provide a good solution. But yeah, using an extra field sounds reasonable.
    – MoritzLost
    Commented Nov 15, 2021 at 15:56
  • 1
    @HonsaStunna If it's a onepager, you can just use the entry slugs as fragments. Just output an ID based on the entry slug in your HTML code, then you can link to the specific sections by building a link with the slug as the fragment.
    – MoritzLost
    Commented Nov 16, 2021 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.