How does one pass a value from a JavaScript function on an iframe to its parent?
-
This is the line that calls the function on the iframe: document.getElementById("mpa").contentWindow.codeAddress(document.myform.address_street1.value); This function returns a value, but i can´t assign this value in the parent. The function in the iframe is: function codeAddress(address) { geocoder.geocode( { 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location}); address = results[0].geometry.location.toString(); return address; } else { alert("Geocode was not successful for the following reason: " + status); } }); } The function works right, but i can´t do this in the parent window: str = document.getElementById("mpa").contentWindow.codeAddress(document.myform.address_street1.value); At the end it returns str undefined.
-
Answer:
Rich if both iframes are under the same domain and the function defined in the iframe is attached to the global object (window) then what you provided should work fine. I've created a simple jfiddle example: the parent: http://jsfiddle.net/XngwC/11/ and the iframe: http://jsfiddle.net/62va5/2/ and: document.getElementById("iframe").contentWindow.sayHello(); executes and returns the expected value.
Michal Kuklis at Quora Visit the source
Related Q & A:
- how to estimate the phase parameter of a complex function?Best solution by Mathematics
- How to connect to a Pervasive Database using javascript?Best solution by Stack Overflow
- How to pass a parameter to a function that is called on an event?Best solution by Stack Overflow
- How can I pass global variables into a function?Best solution by Stack Overflow
- How do I get a value from a JObject?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.