开发者

Multiple Javascript Interpreters in a single browser tab

开发者 https://www.devze.com 2023-02-07 15:44 出处:网络
Is it possible to have multiple javascri开发者_开发技巧pt interpreters(engines) running in a single tab of a browser window? How?

Is it possible to have multiple javascri开发者_开发技巧pt interpreters(engines) running in a single tab of a browser window? How?

I have looked into iframe but have not been able to invoke a separate instance of the javascript interpreter.


You may want to look at Web Workers. These partition execution of Javascript, but have a very defined way of interaction.


Each distinct page has its own global environment. Related pages are linked in various ways (window.parent, the frame elements, etc). If a parent page includes a secondary page as the contents of an <iframe> tag (or in an old-style frameset setup, which I haven't done in well over a decade :-) then the pages in frames each have their own distinct window object, and their own copies of frameworks, tools, etc.

When you do something like document.getElementById("whatever"), that's limited to the global context from which it's called. Similarly, each frame has its own JavaScript environment, with its own distinct copy of the various JavaScript "native" constructors. That is, "Array" in one frame is a distinct object from "Array" in another frame.

@Jarrod N's mention of web workers may also be of great interest to you.


As Pointy said, each distinct page has its own global environment. So iframe also has its own Javascript environment.

My tests with iframe were failing because I was not waiting for the Javascript/webpage in the iframe to get fully loaded, before invoking it from the main page.

p.s. WebWorkers was slightly complicated and has less support for what I wanted for my project. It looks to be a good solution for future projects though.

0

精彩评论

暂无评论...
验证码 换一张
取 消