我开发了一个简单的nw.js(前node-webkit)应用程序。 它打开一个带有属性的全屏 Iframe
nwfaketop
在这个 iframe 中(逻辑上在不同的域中)我需要调用父 Nodejs 函数。 我能怎么做? 这是我的 index.html 主要 nw.js 应用程序的代码:
<html>
<body style="margin:0">
<iframe src="https://www.mydomain.it/test.html" style="width:100%;height:100%;border:0;padding:0" nwfaketop></iframe>
<script>
var gui = require('nw.gui');
var win = gui.Window.get();
win.showDevTools();
var clipboard = gui.Clipboard.get();
function copy_do(text){
clipboard.set(text, 'text');
}
onload = function() {
win.maximize();
}
</script>
</body>
</html>
这里是 iframe 内的代码 https://mydomain.it/test.html
<!doctype html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Test page</title>
</head>
<body>
<div style="text-align: center;">Test page</div>
<script>
parent.copy_do("stackoverflow");
</script>
</body>
</html>
如你所见,我需要调用函数
copy_do();
来自父主文件。
我该怎么办? 提前致谢
最佳答案
您可以将主应用程序中的回调函数注册到 iframe 的内容窗口中。然后从 iframe 中调用该函数。 示例:
在您的主应用程序中:
function copy_do(text){
clipboard.set(text, 'text');
}
var iframe = document.getElementsByTagName('iframe')[0];
iframe.onload = function(){
iframe.contentWindow.myCallback = copy_do;
}
在您的 iframe 脚本中:
window.myCallback('sometext');
关于javascript - Node-webkit iframe 访问父 nw 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341591/