2

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.

3
  • possible duplicate of Set variable in parent window from iframe Commented Aug 16, 2013 at 9:17
  • yes, both are in the same domain Commented Aug 16, 2013 at 9:19
  • Your code works as it is. If you're using Chrome and run this in a local machine, you need to see this SO answer. Notice also, that it's possible, that the function within iframe is not executed at a time you use myvalue in the main window. This appears for example if you read myvalue within $(document).ready(); in the main window, or have placed your at parsing time executed script just before closing body tag. Commented Aug 16, 2013 at 10:01

2 Answers 2

1

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'];
Sign up to request clarification or add additional context in comments.

Comments

0

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.