Get anchor part of URL in controller in Ruby on Rails

倖福魔咒の 提交于 2019-11-26 17:40:31

问题


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?


回答1:


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




回答2:


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];


来源:https://stackoverflow.com/questions/4108082/get-anchor-part-of-url-in-controller-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!