在Ruby on Rails中获取控制器URL的锚点部分

时间:2022-11-27 17:55:28

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

是否有方法在控制器中获取URL的锚点部分?

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

示例:如果我输入http://www.foo.com/bar#anchor123,我可以在我的控制器中获取字符串anchor123吗?

2 个解决方案

#1


39  

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

在Rails中不能这样做,因为锚没有发送到服务器。看到mikeduncan.com/?s=named +锚

#2


30  

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

不,对不起,不可能从服务器端(任何语言)检索#anchor。

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...

但是您可以在主体中使用一些Javascript检查锚点并使用ajax调用将其发送回服务器……

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

#1


39  

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

在Rails中不能这样做,因为锚没有发送到服务器。看到mikeduncan.com/?s=named +锚

#2


30  

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

不,对不起,不可能从服务器端(任何语言)检索#anchor。

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...

但是您可以在主体中使用一些Javascript检查锚点并使用ajax调用将其发送回服务器……

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