decodeURI将空间解码为+符号 [英] decodeURI decodes space as + symbol

查看:386
本文介绍了decodeURI将空间解码为+符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Google自定义搜索。逻辑是当用户搜索时,页面将显示结果,并且页面的标题将使用javascript更改为搜索词。我使用decodeURI解码unicode字符。但是空间被解码为+。例如,如果我搜索钱,它将被解码为钱+制作,并显示为标题。有人请帮忙解决这个问题。我想显示空格而不是符号+。

I have created a Google Custom Search. The logic is when a user search, the page will display result and the title of the page will be changed to the search term using javascript. I use decodeURI to decode the unicode characters. But the space is decode as +. For example if I search money making it will be decoded as money+making and it is displayed as title. Someone please help to solve this. I want to display space instead of the symbol +.

代码是

 if (query != null){document.title = decodeURI(query)+" | Tamil Search";}</script>


推荐答案

Google Closure Library 提供了自己的urlDecode函数。您可以使用该库,也可以使用下面的开源解决方案他们如何解决它。

The Google Closure Library provides its own urlDecode function for just this reason. You can either use the library or below is the open source solution for how they solve it.

/**
 * URL-decodes the string. We need to specially handle '+'s because
 * the javascript library doesn't convert them to spaces.
 * @param {string} str The string to url decode.
 * @return {string} The decoded {@code str}.
 */
goog.string.urlDecode = function(str) {
  return decodeURIComponent(str.replace(/\+/g, ' '));
};

这篇关于decodeURI将空间解码为+符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆