select_tag 与 '/' 的 OnChange 问题和'。' Rails 中的字符

发布于 2024-08-06 12:20:45 字数 305 浏览 1 评论 0原文

我有一个 select_tag (rails),它加载了带有“/”和“.”的选择值其中的人物。

select 标记的 onchange 使用连接的 url 和 select 标记的值来重定向页面(使用 window.location)。

这些是我尝试隔离问题的方法。

  • 调用警报 javascript 以显示连接值。
  • 将 urlencode 添加到传递的值中。
  • 将 urlencode 值硬编码到 windows.location (奇迹般地,它正在工作)。

提前致谢。

I have an select_tag (rails) that loaded a select value with '/' and '.' character in it.

The select tag's onchange redirects the page (using window.location) using concatenated url with the value of the select tag.

These are the things I tried to isolate the problem.

  • Invokes alert javascript to show the concatenated value.
  • Added urlencode to the value passed.
  • Hard code the urlencode'd value to windows.location (and miraculously, it is working).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-08-13 12:20:45

没有代码就很难说(或者确实是一个实际问题!),但一般来说,当您从文本值构建 URL 时,您应该在客户端调用encodeURIComponent,而不是尝试在服务器端对其进行编码。例如。

<select id="foo">
    <option value="123./">123./</option>
    <option value="456./">456./</option>
</select>

<script type="text/javascript">
    document.getElementById('foo').onchange= function() {
        var v= this.value;
        location.href= 'http://www.example.com/thing?foo='+encodeURIComponent(v);
    };
</script>

但是,./ 都不是查询参数中的特殊字符,因此在这种情况下忘记对它们进行编码实际上不会导致问题。如果我们讨论的是 URL 的路径部分,那么 / 是特殊的,必须进行编码:

location.href= 'http://www.example.com/things/'+encodeURIComponent(v)+'/view';

http://www.example.com/things/123.%2F/view

但是,在路径中包含 %2F 在 Apache 中往往会很麻烦和 IIS 出于各种晦涩且烦人的原因;最好避免这种情况。

无论如何,选择重定向页面的标签都是坏消息,不应该真正使用。除了要求 JS 工作之外,它们还很不方便(没有在新选项卡中打开、书签链接等),它们破坏了键盘导航(因为 onchange 会立即触发,因此无法选择其他选项)并且它们破坏了后退按钮(因为当您返回页面时,表单值会被记住,因此您无法再次更改为相同的页面,因为 onchange 不会触发)。

选择导航是一种不可信的技术。今天,您可以尝试点击弹出窗口

,其中包含简单的链接。

Difficult to say without code (or indeed an actual question!), but in general when you build a URL from a text value, you should be calling encodeURIComponent at the client side, rather than trying to encode it at the server side. eg.

<select id="foo">
    <option value="123./">123./</option>
    <option value="456./">456./</option>
</select>

<script type="text/javascript">
    document.getElementById('foo').onchange= function() {
        var v= this.value;
        location.href= 'http://www.example.com/thing?foo='+encodeURIComponent(v);
    };
</script>

However, neither . nor / are special characters in query parameters, so forgetting to encode them in this case would not actually cause a problem. If we're talking about the path part of a URL then / is special and would have to be encoded:

location.href= 'http://www.example.com/things/'+encodeURIComponent(v)+'/view';

http://www.example.com/things/123.%2F/view

However including %2F in paths tends to be troublesome in both Apache and IIS for various obscure and annoying reasons; it is best avoided.

In any case, select tags that redirect the page are bad news and shouldn't really be used. Apart from requiring JS to work at all, they're inconvenient (no open-in-new-tab, bookmark link, etc.), they break keyboard navigation (because onchange is fired immediately, making it impossible to pick another option) and they break the back button (because when you return to the page, the form values are remembered, so you can't change to the same page forwards again as onchange will not fire).

Select-for-navigation is a discredited technique. Today you might instead try a pop-up-on-click <div> full of simple links instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文