浏览器会为 iframe 提供一个单独的 JavaScript 线程吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11510483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:28:11  来源:igfitidea点击:

Will a browser give an iframe a separate thread for JavaScript?

javascriptmultithreadingbrowser

提问by jedatu

Do web browsers use separate executional threads for JavaScript in iframes?

Web 浏览器是否为 iframe 中的 JavaScript 使用单独的执行线程?

I believe Chrome uses separate threads for each tab, so I am guessing that JavaScript in an iframe would share the same thread as its parent window, however, that seems like a security risk too.

我相信 Chrome 为每个选项卡使用单独的线程,所以我猜测 iframe 中的 JavaScript 将与其父窗口共享相同的线程,但是,这似乎也存在安全风险。

采纳答案by Rob Trickey

Before chrome came along, all tabs of any browser shared the same single thread of JavaScript. Chrome upped the game here, and some others have since followed suit.

在 Chrome 出现之前,任何浏览器的所有选项卡都共享同一个 JavaScript 单线程。Chrome 在这里升级了游戏,其他一些人也纷纷效仿。

This is a browser implementation detail, so there is no solid answer. Older browsers definitely don't. I don't know of any browser that definitely uses another thread for iframes, but to be honest I've never really looked into it.

这是浏览器实现细节,因此没有可靠的答案。较旧的浏览器肯定不会。我不知道任何浏览器肯定使用另一个线程来处理 iframe,但老实说我从来没有真正研究过它。

It isn't a security risk, as no objects are brought along with the thread execution.

这不是安全风险,因为线程执行不会带来任何对象。

回答by mxro

To sum up the other answers: No, iFrames usually run in the same thread/process as the main page.

总结其他答案:不,iFrame 通常在与主页相同的线程/进程中运行

However, it appears the Chromium team are working on further isolation in this area:

但是,Chromium 团队似乎正在努力进一步隔离该领域:

Chromium Issue 99379: Out of process iframes[sorry, link not working - if you can find a link to the issue that works, please let me know]

Chromium 问题 99379:进程外 iframe[抱歉,链接不起作用 - 如果您能找到指向该问题的链接,请告诉我]

Design Plans for Out-of-Process iframes

进程外 iframe 的设计计划

回答by WearyMonkey

Recently tested if JavaScript running in a iFrame would block JavaScript from running in the parent window.

最近测试了在 iFrame 中运行的 JavaScript 是否会阻止 JavaScript 在父窗口中运行。

iFrame on same domain as parent:

iFrame 与父域位于同一域:

  • Chrome 68.0.3440.84: Blocks
  • Safari 11.0.2 (13604.4.7.1.3): Blocks
  • Firefox 61.0.1: Blocks
  • Chrome 68.0.3440.84:块
  • Safari 11.0.2 (13604.4.7.1.3):块
  • Firefox 61.0.1:块

iFrame on different domain as parent

作为父级在不同域上的 iFrame

  • Chrome 68.0.3440.84: Doesn't block
  • Safari 11.0.2 (13604.4.7.1.3): Blocks
  • Firefox 61.0.1: Blocks
  • Chrome 68.0.3440.84:不阻止
  • Safari 11.0.2 (13604.4.7.1.3):块
  • Firefox 61.0.1:块

parent.html:

父.html:

    <body>
    <div id="count"></div>
    <iframe src="./spin.html"></iframe>     
    <script>
        let i = 0;
        let div = document.getElementById("count");
        setInterval(() => {
            div.innerText = i++;
        }, 100);
    </script>
    </body>

spin.html:

旋转.html:

    <body>
    <button id="spin">spin</button>
    <script>
        const spin = document.getElementById("spin");
        spin.addEventListener('click', () => {
            const start = Date.now();
            while (Date.now() - start < 1000) { }
        })
    </script>
    </body>

回答by Relu Mesaros

I've had the same question myself this night, before checking for any existing answers. In the project I'm currently working we have to load an iFrame that uses a different framework and I was curios if that iFrame could somehow block the thread and affect my app. The answer is yes, it can.

在检查任何现有答案之前,我今晚自己也有同样的问题。在我目前正在工作的项目中,我们必须加载一个使用不同框架的 iFrame,如果 iFrame 能以某种方式阻塞线程并影响我的应用程序,我很想知道。答案是肯定的,可以。

My test was done in Chrome. In the parent I've loaded a child iFrame. In the parent I've set an interval to console.log a text every amount time. Then in the iFrame I've used a timeout to launch a 'while' that blocks the thread. The answer: the iFrame uses the same thread.

我的测试是在 Chrome 中完成的。在父级中,我加载了一个子 iFrame。在父级中,我设置了一个间隔来 console.log 每次都记录一个文本。然后在 iFrame 中,我使用超时来启动阻塞线程的“while”。答案是:iFrame 使用相同的线程。

Example:

例子:

In the parent:

在父级中:

setInterval(() => {
  console.log('iFrame still using the thread');
}, 3000)

In the iFrame:

在 iFrame 中:

setTimeout(() => {
  console.log('now the thread is not working in the iFrame anymore');
  while (true) {
  }
}, 10000)

回答by matpop

Late on this but... good point, cause iframe js seems to be concurrent in Firefox 16.
Try with alert function (blocking), you'll see dialogs opening together.
You won't see that in Chrome or IE.
iframe js may access the parent window in Firefox 16 as usual, so I can think of possible race conditions arising.

迟到了,但是...好点,因为 iframe js 在 Firefox 16 中似乎是并发的。
尝试使用警报功能(阻塞),你会看到对话框一起打开。
你不会在 Chrome 或 IE 中看到它。
iframe js 可以像往常一样访问 Firefox 16 中的父窗口,所以我可以想到可能出现的竞争条件。

回答by JoeyP

Did some experimenting with this today in Chrome 28 in Ubuntu. Used this command to see Chrome's threads and processes

今天在 Ubuntu 的 Chrome 28 中做了一些实验。使用此命令查看 Chrome 的线程和进程

ps axo pid,nlwp,cmd | grep "chrome"

ps axo pid,nlwp,cmd | grep "chrome"

It looks like Chrome does not spawn new threads or processes for iframes. An interesting note is that it does spawn a new process for the dev tools pane.

看起来 Chrome 不会为 iframe 生成新线程或进程。一个有趣的注意事项是它确实为开发工具窗格生成了一个新进程。

回答by Stev0

For iFrames, no. However if you want to use threads in JavaScript you can use Web Workers, a working html5 draft supported by the new browsers. http://www.w3.org/TR/2009/WD-workers-20091029/

对于 iFrame,没有。但是,如果您想在 JavaScript 中使用线程,您可以使用 Web Workers,这是一种新浏览器支持的有效 html5 草案。http://www.w3.org/TR/2009/WD-workers-20091029/