Do Frames And Iframes Have Isolated Javascript Contexts?
Solution 1:
Yes.
However, you can use the frames
collection or the parent
to access other frames (assuming they're from the same domain).
Solution 2:
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:
- Create a JavaScript object in frame A.
- Pass the JavaScript object to a function in frame B, which saves the value somewhere (in frame B)
- Frame A is reloaded with a new page
- 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.
Solution 3:
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/
Solution 4:
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.
Post a Comment for "Do Frames And Iframes Have Isolated Javascript Contexts?"