iFrame 隔离

发布于 2024-08-29 07:58:15 字数 121 浏览 3 评论 0原文

只是一个想法,但在 DIV 上使用 IFRAME 本质上会使该元素与窗口隔离,从而减慢 IFRAME 中运行的脚本> 不会影响其他框架/窗口吗?

Just a thought, but would using an IFRAME over a DIV essentially make that element isolated from the window in a way that slow scripts running in the IFRAME wouldn't affect the other frames/window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞄了个咪的 2024-09-05 07:58:15

是的,对于第一部分,iframe 会“某种程度上”将您的窗口与 iframe 中的脚本隔离开来。但是,仍然可以通过 window.parent 访问父窗口。

对于第二部分:不,它不会使其如此慢,iframe 中的脚本不会影响其他框架/窗口。您的主窗口对象及其子节点都在同一线程中运行。 JavaScript 是单线程的[在这种情况下忽略 webworkers,无论如何你都无法在它们之间传递 dom 元素],因此你可以访问父窗口/子 iframe 的窗口对象的唯一原因是因为它们位于同一线程上。

提供一个简单的示例:

  • 创建一个名为 main.html 的页面
  • 在该页面中,有一个 iframe src="iframe.html"
  • 在 iframe 旁边,有一个按钮,其中包含您想要的任何文本,我不知道不在乎。
  • 在 iframe.html 中,window.onload = function(){ while(1){} };
  • 访问 iframe.html。您会注意到,当您将鼠标光标放在按钮上时,它不会响应/重绘。这是因为浏览器被冻结了。

来源:
我也尝试过像这样的多线程。艰难地学到了=)

Yes for the first part, an iframe will "sort-of" isolate your window from the script in the iframe. However, the parent window can still be accessed via window.parent.

For the second part: No, it will not make it so slow scripts in the iframe won't affect other frames/windows. Your main window object and its child nodes all run in the same thread. JavaScript is single threaded [Ignore webworkers in this case, you can't pass dom elements between them anyway], so the only reason you can access the parent-window/child-iframe's window object is because they're on the same thread.

To provide a quick example:

  • Create a page called main.html
  • In that page, have an iframe src="iframe.html"
  • Next to the iframe, have a button with whatever text you want, I don't care.
  • In iframe.html, window.onload = function(){ while(1){} };
  • Access iframe.html. You'll notice that when you put your mouse cursor over the button, it doesn't respond/redraw. This is because the browser is frozen.

Source:
I tried getting multithreading like this too. Learned the hard way =)

爱的十字路口 2024-09-05 07:58:15

在新浏览器中,您可以使用沙箱属性将 iframe 与页面的其余部分隔离

http:// www.w3schools.com/tags/att_iframe_sandbox.asp

In new browsers you can use the sandbox property to isolate the iframe from the rest of the page

http://www.w3schools.com/tags/att_iframe_sandbox.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文