user619804
user619804

Reputation: 2356

Add js function to iframe - dojo

We create an iFrame like

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

We'd like to have insert an additional javascript function like

function printThis() { window.print(); }

in the iFrame so that in the parent window we can call the printThis() function from some code like

_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;
},

-is this possible? I know that dojo.io.iframe.create(generatedRequestId) takes a second param that would be code that would be executed onLoad - but not necessarily a function that can be called after iframe loads?

Thanks for any advice.

Upvotes: 0

Views: 888

Answers (1)

zetlen
zetlen

Reputation: 3619

When you declare a "global" variable or function in browser JavaScript, the variable is available as a property of the window object. If the iframe is from the same origin, then you have access to its window object through the contentWindow property of the iframe.

iframe.contentWindow.printThis();

Upvotes: 1

Related Questions