调用window.open打开bprint://意图时URL冒号丢失,如何保留原URL?
解决自定义URL Scheme拼接时冒号丢失的问题
URL编码目标地址:直接对需要拼接的HTTPS地址进行URL编码,避免浏览器自动处理特殊字符。示例代码:
const targetUrl = encodeURIComponent("https://www.example.com"); window.open(`bprint://${targetUrl}`, '_blank');编码后目标地址会转为
https%3A%2F%2Fwww.example.com,拼接后的完整URL为bprint://https%3A%2F%2Fwww.example.com,打印应用通常会自动解码识别原地址。通过临时页面跳转:如果编码方式不生效,可以创建一个临时HTML页面,在页面内执行跳转,绕过浏览器对自定义Scheme的解析限制。示例代码:
const html = `<html><head><script>window.location.href = 'bprint://https://www.example.com';</script></head></html>`; const blob = new Blob([html], { type: 'text/html' }); const blobUrl = URL.createObjectURL(blob); window.open(blobUrl, '_blank'); // 用完记得释放资源 setTimeout(() => URL.revokeObjectURL(blobUrl), 1000);这种方式通过本地临时页面发起跳转,浏览器不会修改跳转链接里的冒号。
内容的提问来源于stack exchange,提问作者Gabriel G
相关产品推荐
相关产品推荐

