【问题标题】:Backbone - URL Encoded with diacritics in JavaScriptBackbone - 在 JavaScript 中使用变音符号编码的 URL
【发布时间】:2016-10-06 08:19:49
【问题描述】:

我有一个搜索表单将用户重定向到search/thetermusersearched,其中thetermusersearched 是他输入的确切值。这一切都归功于 Backbone 提供的导航功能。

当我在英语中使用字符串 (masa de calcat) 时这一切都很好,但是当我在输入中添加变音符号时 (masă de călcat) 我得到 route 函数被触发两次。

我遇到的问题是 Firefox 和 Safari(Mac 和 iOS 中的后者)

当我使用navigate 时,我尝试使用encodeURIencodeURIComponent,但没有成功。


HTML

<div id="view-goes-here">
  <a href="#" data-string="masa de calcat">One alert</a>
  <a href="#" data-string="masă de călcat">Two alerts</a>
</div>

JS

var R = Backbone.Router.extend({
    routes: {
        'results/:query': 'results'
    },
    results: function(query) {
        alert('Route triggered: ' + decodeURIComponent(query));
    }
});
var myR = new R;
Backbone.history.start();

$(function(){
  $('a').on('click', function(e){
   e.preventDefault();
   var href = $(this).data('string');
   href = 'results/' + encodeURIComponent(href);
   console.log(href);
   myR.navigate(href, {trigger: true});
  })
})

在此处查看小提琴:http://jsfiddle.net/adyz/qcged76e/4/

对此有什么想法吗?

【问题讨论】:

    标签: javascript html backbone.js


    【解决方案1】:

    我在您的小提琴中收到两个链接的双重警报。 我尝试在编码后使用转义,这似乎有效:

    href =  'results/' + escape(encodeURIComponent(href));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      相关资源
      最近更新 更多