SO中文参考
首页
(current)
程序语言
c
java
python
c++
go
javascript
swift
c#
操作系统
linux
ubuntu
centos
unix
数据库
oracle
mysql
mongodb
postgresql
框架
node.js
angular
react-native
avalon
django
twisted
hadoop
.net
移动开发
android
ios
搜索
清除iframe内容(包括它的JS全局范围)你好世界<\/title><\/head><body><h1>你好世界<\/h1><scri...</desc> <question vote="4"> <p>我正在像这样动态创建一个 iframe:</p> <pre><code>let code = "<html><head><title> hello world <\/title><\/head><body><h1> hello world <\/h1><script>const h1 = document.querySelector('h1'); h1.style.color = 'red'<\/script><\/body><\/html>" const iframe = document.createElement('iframe') document.body.appendChild(iframe) const content = iframe.contentDocument || iframe.contentWindow.document content.open() content.write(code) content.close() </code></pre> <p>然后我将其内容更改为这样:</p> <pre><code>code = "<html><head><title> bye world <\/title><\/head><body><h1> bye world <\/h1><script>const h1 = document.querySelector('h1'); h1.style.color = 'red'<\/script><\/body><\/html>" content.open() content.write(code) content.close() </code></pre> <p>但我收到以下错误:<pre><code>SyntaxError: redeclaration of const h1</code></pre>,因为 JavaScript 被注入到 iframe 中。</p> <p>在向 iframe 写入新代码之前,有什么方法可以清除 iframe 内的全局变量范围吗?我已经尝试过 <pre><code>iframe.contentWindow.location.replace('about:blank')</code></pre> 以及 <pre><code>iframe.src = 'about:blank'</code></pre> 但这并不能清除它的内部 JS 变量范围。 (我也遇到同样的错误)</p> <p>我意识到我可以废弃整个 iframe 并创建一个新的 iframe,但我将每秒更换几次代码,并且我希望有一种成本更低的方法来更新它。</p> </question> <answer tick="false" vote="0"> <p>您可以用花括号 <pre><code>const</code></pre> 将动态 <pre><code>let</code></pre> 变量中的 <pre><code>content</code></pre> 和 <pre><code>{}</code></pre> 声明括起来,创建块作用域。这是解决重新声明错误的简单方法。</p> <pre><code>// Note the added braces {} within the <script> let code = "<html><head><title> hello world <\/title><\/head><body><h1> hello world <\/h1><script>{ const h1 = document.querySelector('h1'); h1.style.color = 'red' }<\/script><\/body><\/html>" </code></pre> <p>可能更好的方法是首先删除 DOM 中已存在的所有 iframe,然后在需要添加内容时重新创建 <pre><code>iframe</code></pre>。</p> <pre><code>const updateIframe = (code) => { const iframe = document.createElement('iframe') document.body.querySelector('iframe')?.remove() document.body.appendChild(iframe) const content = iframe.contentDocument || iframe.contentWindow.document content.open() content.write(code) content.close() } </code></pre> </answer> </body></html>
问题描述
投票:0
回答:0
javascript
variables
iframe
scope
最新问题
未捕获的引用错误:(函数)未定义于
Android 客户端的 Django Rest Framework 响应的通用 JSON 格式
ReferenceError:__dirname 未在 ES 模块作用域中定义
在ajax上发送多个输入
从配置了客户端预取的 Artemis 队列中删除所有消息?
无需 Microsoft API 即可访问 Outlook 邮件 MIME 内容:可能吗?
交换 GList (GLib) 中的两个项目
为什么选择排序最佳情况表示法(Omega 表示法)是 n^2 而不仅仅是 n?
如果我尝试添加淡入,则在视频上叠加 PNG 不起作用,但无需淡入即可工作
为什么`set`对象不像其他内置容器类型那样使用`__newobj__`函数来unpickle?
使用 Pick 检索特定类型的所有密钥
R 中的 ODBC/DBI 不会写入 R 中具有非默认模式的表
从 WinLib 安装最新的 GCC 和 Clang 会导致缺少 dll 错误
无法识别我的 Jupyter Notebook 中导入的包的版本
根据字典中的索引合并两个字典列表
计算现在与过去的 Excel 日期时间戳记之间的年、月和日
如何在CQRS中提取业务规则验证?
如何在Windows中正确删除(删除或禁用)权限?
如何使用 R Shiny 将命名空间模块的 UI 干净地注入到主应用程序的 UI 中?
Spring Boot 使用 @EnableCaching 的默认缓存管理器
© www.soinside.com 2019 - 2024. All rights reserved.