繁体   English   中英

如何从弹出窗口调用父函数?

[英]How to call the parent function from a popup window?

我有一个showMap函数,它会打开一个新窗口(window.open)。在新窗口中,我有一个iframe googlemaps和一个带按钮的输入。当用户在父窗口中单击按钮时如何传递数据?

var popUpWindow ;
function showMap(){
     popUpWindow = window.open(index)
     popUpWindow.document.write( '<html><head><title>MAP</title>
     </head><body>
     <iframe src="https://maps.google.com/maps/embed/v1/directions?> 
     </iframe> <br> <input type="text" id="data> 
     <button type="button" onclick="opener.callParent()">
      Update</button></body></html');
}

function callParent(){
  var newValue = popUpWindow.document.getElementById('data').value;
  console.log('newVal',newValue);
}

通过这种方式,我能够从子窗口到父窗口获取值。

在新弹出窗口下检查window.opener全局

您可以使用ES6导入/导出。

为此,首先导出父级函数,然后导入它并在模式中使用。

另一种方法,但很危险的一种方法是使用全局窗口范围来实现您要调用的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM