将js函数添加到iframe - dojo

时间:2012-11-11 13:46:11

标签: javascript iframe dojo

我们创建了一个类似

的iFrame
var iframe = dojo.io.iframe.create(generatedRequestId);

我们想插入一个额外的javascript函数,如

function 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加载后可以调用的函数吗?

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

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

iframe.contentWindow.printThis();
相关问题