简体   繁体   中英

How to define javascript variable from within a iframe

I have an iframe with that needs to change a global variable in the parent page.

I tried

parent.myvar = "myvalue";
parent.myfunction();

However, it's not working for me. The function executes but the variable remains unchanged.

EDIT: both the iframe and the parent are in the same domain.

Not possible if the iframe and the main document are not in the same domain.

If iframe and main document are in the same domain, you can access their global variables. Try this maybe.

document.getElementById('iframeid').contentWindow['myvar'];

There is nothing wrong with that code : https://stackoverflow.com/a/1301645/390330

// set the global variable 'foo' in the parent global scope
parent.foo = 'bar';

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