javascript – decodeURI将空间解码为符号

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

代码

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

解决方法

出于这个原因,Google Closure Library提供了自己的urlDecode函数.您可以使用该库,或者下面是 open source solution,以了解它们如何解决它.
/**
 * 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,' '));
};

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...