【问题标题】:How to call the parent function from a popup window?如何从弹出窗口调用父函数?
【发布时间】:2019-06-07 21:05:43
【问题描述】:

我有一个函数 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);
}

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

【问题讨论】:

标签: javascript jquery


【解决方案1】:

在该新弹出窗口下检查window.opener global

【讨论】:

【解决方案2】:

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

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

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

【讨论】:

  • 我想在翡翠中实现这个。所以,我不能作为 ES6 导入
猜你喜欢
  • 2021-01-08
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多