You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS实现Spotify搜索跳转 西里尔字符URL编码报错求解

问题根源
  • 代码中使用的escape()是JavaScript早期遗留的废弃函数,不属于Web标准,它对西里尔字母等非ASCII字符会生成%uXXXX格式的非标准编码,不符合URL编码规范,Spotify无法解析这类编码后的内容。
  • decodeURIComponent仅支持标准UTF-8百分号编码(格式为%XX),遇到%uXXXX格式就会直接抛出URI malformed错误,所以你之前的修复方案不可行。
修复方法

把escape()替换为标准API encodeURIComponent()即可,这个方法会按照UTF-8规则生成符合RFC 3986规范的URL编码,Spotify可以正常识别包含西里尔字母在内的所有字符。

修复后的可运行代码:

<form name="search">
  <input id="inputSearch" type="text" name="search" placeholder="enter track">
  <button id="btnSearch" type="button" onclick="window.open('https://open.spotify.com/search/'+encodeURIComponent(document.forms['search'].elements['search'].value), '_blank')">search</button>
</form>
可选优化

如果要支持按回车键触发搜索,可以把表单改为监听submit事件,不用额外绑定回车事件:

<form name="search" onsubmit="window.open('https://open.spotify.com/search/'+encodeURIComponent(this.elements['search'].value), '_blank'); return false;">
  <input id="inputSearch" type="text" name="search" placeholder="enter track">
  <button id="btnSearch" type="submit">search</button>
</form>

注:不要对编码后的结果额外调用解码方法,标准URL编码会被浏览器和服务端自动识别处理,多余的解码操作反而会导致乱码。


内容的提问来源于stack exchange,提问作者davy jones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.04 08:33:32