Contents |
Scripting your ManagedIframeThere are several methods available in the ManagedIframe interface that permit interaction with the embedded frame's document. However, such interaction with the embedded document is only permitted when
Note: any attempt to execute one of the following functions, on an embedded document loaded from a "foreign domain" will throw an 'execScript:non-secure context' exception. (see the :domWritable method for more details.)
managedIframe:execScriptallows the hosting page of the IFRAME to execute (eval) JavaScript source in the IFRAME's window context.
Syntax:
execScript( source [, useDom ])
Parameter Description:
Example:
mif.execScript("location.href="+newUrl);
This statement would be executed in the scope of the frames' Window object and changes the page address (location) of the embedded page.
Remote Script Load example:
MIF.load({ url:'otherLocalPage.html',
params: {customer: 2398 },
success : function(){
Ext.Ajax.request({
url :'libs/YUI.js',
success: function(response){
MIF.execScript( response.responseText ); }
});
}
});
The previous example would make an Ajax request to retrieve the YUI library and execute(eval) it in the embedded frames Window context after the local document has been rendered into the frame document.
managedIframe:loadFunctionThe :loadFunction method permits the transfer of an existing function defined within the host-pages Window context to that of the embedded window context and optionally executes it.
Syntax:
loadFunction( fnName [, useDom ][, invokeIt])
loadFunction( { name: fnName, fn: function-definition } [, useDom ][, invokeIt])
Parameter Description:
Example: basic form
// Eval a trim function definition into the iframe window context.
// Define the portable trim function:
var trim = function(s){
return s.replace( /^\s+|\s+$/g,"");
};
MIF.loadFunction('trim', true); //enclose in a <script> tag
Example: function map form:
MIF.loadFunction({name:'myTrim',fn: String.prototype.trim || trim}, true );
MIF.execScript('alert(myTrim(" Test Trim "))');
The previous example gathers the function definition from 'String.prototype.trim' or the previously-defined 'trim' function and changes its name to 'myTrim' for future execution in the embedded frame.
Advanced Scripting ExampleYou could for example, execute a graphing package in the IFRAME's window context (without affecting the host page at all):
var graph_master_src;
Ext.Ajax.request({
url:'graphmaster.js'
,success: function(response){
graph_master_src = response.responseText; }
});
// then, later.....
var graphIt = function(){
mIframe.execScript(graph_master_src); //load the chart conversion script.
mIframe.execScript('new graph_master().applyTo("salesResults");'); //apply it to the table
mIframe.print(); // now print it!
};
//As soon as its DOM is ready, execute the chart script
mIframe.on('documentloaded',graphIt);
mIframe.load('graphTable.php'); //generate the results as an HTML table