46

Is there a way to get the anchor part of a URL in a controller?

Example: if I type http://www.foo.com/bar#anchor123 can I get the string anchor123 in my controller?

2 Answers 2

48

You can't do that in Rails, because the anchor is not sent to the server. See mikeduncan.com/?s=named+anchors

1
  • 1
    The post is dead again.
    – Backo
    Jan 12, 2019 at 22:12
34

No sorry, it's not possible to retrieve the #anchor from the server-side (in any language).

This is a client-side flag to tell the browser to move to a specific position in the page.

But you can use some Javascript in the body to check for an anchor and send it back to the server using an Ajax-call...

var anchor_value;
var stripped_url = document.location.toString().split("#");
if (stripped_url.length > 1)
  anchor_value = stripped_url[1];
1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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