Pass variable from Iframe to parent

Hello:

I have a multiple select box inside an iframe in which I added an onchange command so that the data from the select box shows in the text box. It works fine when I keep the textbox in the iframe. However when I move the textbox back to the parent page, it of course does not work.
How do I edit the script below to pass the variable from the iframe to the parent page.

onchange="UpdateFromList (this, this.form.ListSelected);"
LVL 1
RecipeDanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b0lsc0ttIT ManagerCommented:
RecipeDan,

Are the pages both on the same server and domain?  Same protocol too (i.e. neither https)?

You need to pass it to something more specific than just the parent page.  However if it is the input on that page then use ...

onchange="top.document.formName.inputName.value = this.value;"

That should at least let you know how to "communicate" with the parent page.  If you want to call a function on the page or send the value somewhere else then you just need to modify the script a bit.  If you need help with details then please elaborate on the html for the pages and elements.

Let me know if you have any questions or need more information.

b0lsc0tt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b0lsc0ttIT ManagerCommented:
RecipeDan,

By the way ... if the pages are on different domains/servers or the protocols are mixed then the script won't work.  The browser will stop this because it considers it a security issue (cross domain scripting).

Let me know if you have a question.

b0lsc0tt
0
amosvanhornCommented:
Here's a snippet of code that I used to communicate from an iFrame to some code in the Parent page:



// code in iFrame:
 function notifyParentOfChange()
        {
            try
            {
                var thisIFrameName= window.name;
                window.parent.textValueChanged(thisIFrameName, GetTextFromInput());
            }
            catch(e)
            {
                // do nothing if the method does not exist in parent
            }
        }

function GetTextFromInput()
        {
            return document.getElementById("textBox1").value;
        }
//********************************//

Code in Parent:

function textValueChanged(iframeName, newText)
{
    alert("The iFrame " + iframeName + " says the new text value is: " + newText);
}
0
shiraztiCommented:
A simple example, sometimes may helpful to you. ( update() function should be on the iframe page)

      function update() {
            parent.document.formname.textbox.value = 'New Text';
      }
0
b0lsc0ttIT ManagerCommented:
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.