encodeURIComponent对url参数进行编码

编程入门 行业动态 更新时间:2024-10-19 10:24:48

encodeURIComponent对url<a href=https://www.elefans.com/category/jswz/34/1771441.html style=参数进行编码"/>

encodeURIComponent对url参数进行编码

在开发需求过程中,经常会遇到点击链接进入详情页的情况,一般的做法如下:

window.open("/xxx/xxx/xxxDetail?a=" + item.a + '&b=' + item.b);

我们也经常需要在详情页中获取url上面的参数进行一些逻辑的处理,一般的做法如下:

function getHrefParam(key) {const search = window.location.search;const params = new URLSearchParams(search);return (params.get(key)) || '';
}let a = getHrefParam(a)
let b = getHrefParam(b)

特殊情况:

当我们拼接在url上的参数存在某些特殊字符时(&、%、#、?、/ 等),getHrefParam()并不能满足我们的需求,例如:url后面的参数是:?a=xxxx#12&b=xxx&c=xxx

window.location.search方法获取的参数被“#”截断

解决方法:encodeURIComponent对参数进行一次编码即可

window.open("/xxx/xxx/xxxDetail?a=" + encodeURIComponent(item.a) + '&b=' + encodeURIComponent(item.b));

更多推荐

encodeURIComponent对url参数进行编码

本文发布于:2023-12-03 07:30:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1652680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   encodeURIComponent   url

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!