10

I've made some experiments in Chrome but I'm not sure so I need a confirmation :

Am I correct in thinking that iframes and frames have a separate JavaScript context, making them impossible to share variables between those frames/iframes?

To simplify, let's assume that the client will always be the same version of Chrome (it's my case)

Klaim
  • 67,274
  • 36
  • 133
  • 188
  • The answer is "yes" but it's not impossible to "share variables". It can, however, be risky, as Internet Explorer is very picky about the lineage of JavaScript objects. – Pointy Nov 21 '11 at 16:31
  • You should make an answer instead of a comment ;) Also, let's assume the client will always be Chrome. – Klaim Nov 21 '11 at 16:33
  • answer is ready. I don't *think* that Chrome has the sensitivity IE does, but I'm not 100% sure. It'd be pretty easy to test. – Pointy Nov 21 '11 at 16:35

4 Answers4

10

Yes.

However, you can use the frames collection or the parent to access other frames (assuming they're from the same domain).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • This makes me curious. If there are multiple execution contexts, but they have access to one another's DOM (being on the same domain), how do browsers manage multithreading issues between them? In other words, if javascript tries to edit the same DOM element simultaneously from two iframes, is there some kind of locking going on to manage those accesses? Or is the javascript running in those two frames effectively on the same UI thread? – StriplingWarrior Jan 10 '19 at 17:37
  • 1
    All execution contexts that are capable of accessing each-other's windows run on a single thread. This is why you will sometimes see a tab cause multiple other tabs to freeze (eg, while debugging). – SLaks Jan 15 '19 at 19:55
7

It's not "impossible" to share values between frames, but you have to be careful. In Internet Explorer, the following scenario will result in an error:

  1. Create a JavaScript object in frame A.
  2. Pass the JavaScript object to a function in frame B, which saves the value somewhere (in frame B)
  3. Frame A is reloaded with a new page
  4. Code in frame B attempts to reference the saved object from the (former) frame A.

Internet Explorer does not like it when an object from a defunct page is referenced.

Pointy
  • 405,095
  • 59
  • 585
  • 614
5

Well, they just have different global objects and global scope. However, if they are in the same domain you can run code in one from another. But if you were to do this (inside parent window):

document.getElementById( "myiframe" ).contentWindow.window.globalArray = [];

Which creates a global variable globalArray inside the iframe's global scope.

and then inside the iframe

console.log( globalArray instanceof Array );

will return false because Array refers to the iframe's Array constructor. You'd have to do

console.log( globalArray instanceof top.Array );

where top refers to the container window global object.

jsfiddle: http://jsfiddle.net/EFbtN/

Esailija
  • 138,174
  • 23
  • 272
  • 326
1

The separation of context is not between frames, it is between domains. This means that if you load frame A with domain A and frame B with domain B, javascript from frame A cannot access domain B's context. Check this for a lengthier explanation.

EDIT: Of course, if they 're on the same domain, the answer provided by SLaks fully applies.

Ioannis Karadimas
  • 7,746
  • 3
  • 35
  • 45