简体   繁体   中英

Accessing a global variable of iframe from parent window

I am trying to access a global variable (Storage object) defined in iframe window from a parent window. Here is what i have tried but i am getting an Uncaught TypError:

Cannot read property 'Storage' of undefined.

js for iframe with id:iframe

Storage.prototype.removeObject = function (key)
{
    this.removeItem(key);
    console.log('item removed');
}

js for parent window

const iFrame = $('#iframe');
iFrame.contentWindow.Storage.removeItem("mapsTracking");

Any help would be appreciated.

I ran into this issue today. The "window.parent.variableName" did the trick for me.

You can try directly adding the method on Storage

Storage.removeObject = function (key)
{
    this.removeItem(key);
    console.log('item removed');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM