清除URL中的零宽空格
在编码前使用正则表达式移除不可见字符:
javascript
$openWebview = function(url) {
// 移除所有不可见字符(包括零宽空格)
const cleanUrl = url.replace(/[\u200B-\u200D\uFEFF]/g, '');
console.log(encodeURIComponent(cleanUrl)); // 输出正常编码结果
uni.navigateTo({
url: '/pages/webview/webview?url=' + encodeURIComponent(cleanUrl)
});
}
确保调用时传入的URL没有隐藏字符:
javascript
// 正确调用方式(直接输入字符串,不要从富文本等可能含隐藏字符的源复制)
this.$openWebview('http://www.baidu.com');
在webview页面:
javascript
onLoad(e) {
// console.log(JSON.stringify(e.url));
//
// console.log(decodeURIComponent(e.url));
if(e.url){
this.url1 = decodeURIComponent(e.url)
// this.url1 = e.url
console.log(this.url1)
}
// this.url1 ='http://www.baidu.com'
// this.url =(JSON.stringify(e.url))
}