node-webkit iframe访问父nw函数
Node-webkit iframe access to parent nw function
我开发了一个简单的 nw.js(ex 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 中的代码
<!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("Whosebug");
</script>
</body>
</html>
如你所见,我需要调用函数
copy_do();
来自父主文件。
我该怎么办?
提前致谢
您可以从您的主应用程序向您的 iframe 内容注册一个回调函数 window。然后从您的 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');
试试这个。 onload() 函数无法捕获子文档的完整状态。
请检查文档就绪状态为 "complete".
<iframe id="ifrmChild" src="http://yourdomain/iframe.html" style="width:100%;height:100%;border:0;padding:0" nwfaketop></iframe>
<script>
window.doit = function(text){
console.log('Hey!!!!!');
}
var interval = setInterval(function () {
var iframe = document.getElementById("ifrmChild");
var cw = iframe.contentWindow;
if (cw.document.readyState == "complete")
{
clearInterval(interval);
iframe.contentWindow["doit"] = window.doit;
iframe.contentWindow.eval("doit('ssss');");
}
});
我开发了一个简单的 nw.js(ex node-webkit)应用程序。 它打开一个带有属性
的全屏 Iframenwfaketop
在这个 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 中的代码
<!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("Whosebug");
</script>
</body>
</html>
如你所见,我需要调用函数
copy_do();
来自父主文件。
我该怎么办? 提前致谢
您可以从您的主应用程序向您的 iframe 内容注册一个回调函数 window。然后从您的 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');
试试这个。 onload() 函数无法捕获子文档的完整状态。 请检查文档就绪状态为 "complete".
<iframe id="ifrmChild" src="http://yourdomain/iframe.html" style="width:100%;height:100%;border:0;padding:0" nwfaketop></iframe>
<script>
window.doit = function(text){
console.log('Hey!!!!!');
}
var interval = setInterval(function () {
var iframe = document.getElementById("ifrmChild");
var cw = iframe.contentWindow;
if (cw.document.readyState == "complete")
{
clearInterval(interval);
iframe.contentWindow["doit"] = window.doit;
iframe.contentWindow.eval("doit('ssss');");
}
});