iFrame isolation
Asked Answered
N

2

6

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?

Nebulous answered 20/4, 2010 at 4:11 Comment(0)
J
12

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 =)

Jumbled answered 20/4, 2010 at 6:31 Comment(2)
Haha great answer, thanks a lot. Saved me me going down a dead end.Nebulous
correct answer, one small remark though; "However, the parent window can still be accessed via window.parent" is only correct if both parent and child (iframe) are on same domain. if they are not, you indeed could consider the iframe a sandboxed environment due to the "same origin policy".Revert
A
3

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

Agle answered 19/11, 2015 at 4:41 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.