How to pass data to parent window from popup window?
The window.opener object is what you're looking for, used it from within your popup like so to call the a function of the parent window:
window.opener.yourFunc()
Here is a fun and easy demo that is heavily inspired by this answer to a similar question (but modified for my own purposes to help investigate the most difficult bug of my career).
Create 2 files (in the same directory) as follows:
parent.html
<button type="button" onclick="popup('popup.html', '', 800, 200);">Add My Card</button>
=>
<span id="retrievedData">No data yet.</span>
<script>
function popup(url, title, width, height) {
var left = (screen.width / 2) - (width / 2);
var top = (screen.height / 2) - (height / 2);
var options = '';
options += ',width=' + width;
options += ',height=' + height;
options += ',top=' + top;
options += ',left=' + left;
return window.open(url, title, options);
}
function setData(data) {
console.log(data);
var strData = JSON.stringify(data);
document.getElementById('retrievedData').innerHTML = strData;
var requestBinUrl = 'http://requestb.in/18u87g81';
window.location.href = requestBinUrl + '?data=' + strData;
}
</script>
popup.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="popupForm" name="f">
<select id="urlField" name="url">
<option>
http://date.jsontest.com/
</option>
<option>
http://time.jsontest.com/
</option>
<option>
http://md5.jsontest.com/?text=HereIsSomeStuff
</option>
</select>
<div><input type="submit" /></div>
</form>
<script>
$('#popupForm').submit(function(e) {
e.preventDefault();
var url = $('#urlField').val();
console.log(url);
$.ajax({
url: url
}).then(function(data) {
console.log(JSON.stringify(data));
window.opener.setData(data);
window.close();
});
});
</script>