0

我们创建一个 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) 需要第二个参数,它将是 onLoad 执行的代码 - 但不一定是可以在 iframe 加载后调用的函数?

感谢您的任何建议。

4

1 回答 1

1

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

iframe.contentWindow.printThis();
于 2012-11-11T14:07:07.793 回答