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
最新问题
Python 月份的第一个营业日期
Express 不使用 Typescript 导入
Shopify:来自“捆绑安装”的错误
在Expo Go模拟器中的Android中获取“ExponentPushToken”,但在内置的apk版本中 - 我得到了它{“code”:“ERR_UNEXPECTED”}
AWS CLI 命令列出最新 7 个 ami 和快照
从 JSON 文件获取 CSS 颜色变量
SQL XML 列分成多行
如果客户端位于不同的域中,Express 会话不会持续存在
在keras cnn中加载几张没有标签的图像
如何暂停我的代码直到函数返回?
如何在 Windows C++ 应用程序中嵌入 Python 3.12?
如何使用 Playwright 通过 Xpath/CSS 选择器定位元素
VegaLite 条形图中第一个条形以特定颜色突出显示
按周期滚动组总和
如果日期早于 30 天,则使用 bash 删除文本文件中的行
通过 Easy Admin 在 CRUD 控制器中进行新操作“预览”
模拟 ProductCategory findAll 在 Groovy Spock 测试中失败
使用RxJS的interval()函数从服务器获取实时更新是否正确?
在命令提示符下暂停和恢复程序执行
SAS 使用 proc 转置使长种族和种族变量变宽
© www.soinside.com 2019 - 2024. All rights reserved.