应用场景:后端返回支付页面,但改页面为一串加密的html字符串,需要前端进行处理然后渲染到窗口出来。
<html>..<body>...</body><script>..submit</script></html>
业务解决:使用document.write将页面渲染写入,下面是业务代码。
const { Base64 } = await import('js-base64') document.write(Base64.decode(bean.payHtml))
错误提示:
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
解决方法:
翻译后搜索得知当前文档流已经关闭,不允许重新写入。 怀疑是在异步函数async await中调用,越发的事故。使用async await是为了代码拆分,按需引入Base64解密代码,所以将Base64通过import导入,解决问题。