Luke
Luke

Reputation: 5708

Changing the global scope in JS

Say i have an Iframe running inside of a parent window.

I have a single script loaded inside the Iframe and I want that script to have access to the variables inside of its parent.

Rather than saying window.parent.X all the time, is it possible to just declare:

window= window.parent;

inside of the Iframe?

Upvotes: 2

Views: 39

Answers (2)

Paul S.
Paul S.

Reputation: 66334

can you give an example of what you mean by "create a closure that shadows it"

// `window` here works normally

(function (window) {
    // `window` here is what would be `window.parent`
}(window.parent));

// `window` here works normally

Please note that even inside the IIFE's closure you still have the same global object, i.e. anything you don't access via window will not be from the parent window.

Upvotes: 1

taxicala
taxicala

Reputation: 21759

window is a protected variable for js, you will not be able to override it. You can, however, store window.parent in another var and then use that:

var parentWindow = window.parent;

Upvotes: 2

Related Questions