Javascript 将js函数添加到iframe-dojo

Javascript 将js函数添加到iframe-dojo,javascript,iframe,dojo,Javascript,Iframe,Dojo,我们创建了一个类似iFrame的 var iframe = dojo.io.iframe.create(generatedRequestId); 我们希望插入一个额外的javascript函数,如 函数printThis(){ window.print(); } 在iFrame中,以便在父窗口中,我们可以从以下代码调用printThis()函数 _setPrintExportCookieInterval: function(/**String*/requestId, /**function*/

我们创建了一个类似iFrame的

var iframe = dojo.io.iframe.create(generatedRequestId);
我们希望插入一个额外的javascript函数,如

函数printThis(){ window.print(); }

在iFrame中,以便在父窗口中,我们可以从以下代码调用printThis()函数

_setPrintExportCookieInterval: function(/**String*/requestId, /**function*/closePopup, /**String*/exportTypeId) {
    //have the interval autoexpire after some amount of seconds
    var count = 0;
    var intervalMs = 2000;

    var intervalId = self.setInterval(function() {
        var reportCookie = dojo.cookie(requestId);
        if(reportCookie || count > 300000) { //5 mins
            //if there's a status failure, don't close the window
            if(reportCookie == "success") {
                //console.debug(exportTypeId);
                if(exportTypeId == PRINT) {
                    var iframe = dojo.byId(requestId);
                    iframe.printThis();
                }
                closePopup();
            } else {
                console.debug("print/export request returned with nonstandard status " + reportCookie);
            }
            window.clearInterval(intervalId);
            //delete the cookie
            dojo.cookie(requestId, null, {path: "/", expires: -1});
            //destroy the iframe
            //dojo.destroy(dojo.byId(requestId));
        };
        count+=intervalMs;
    }, intervalMs);

    return intervalId;
},
-这可能吗?我知道dojo.io.iframe.create(generatedRequestId)接受第二个参数,该参数是在加载时执行的代码,但不一定是在iframe加载后可以调用的函数

感谢您的建议。

当您在浏览器JavaScript中声明一个“全局”变量或函数时,该变量作为
窗口
对象的属性可用。如果iframe来自同一原点,则可以通过iframe的contentWindow属性访问其
窗口
对象

iframe.contentWindow.printThis();