How to pass the value from the javascript to servlet?

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

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

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.