How can I set height and width of IFRAME in cross domain environment?
How can I access contentDocument
of IFRAME in cross domain environment?
How can I set height and width of IFRAME in cross domain environment?
How can I access contentDocument
of IFRAME in cross domain environment?
We can use IFRAME in cross domain environment. But we have to use an ajax call and IFRAME configurations. Below is the code. It worked for me. I have searched many area. Atlast I got this working code.
HTML
<iframe id="doc-frame" src="javascript:(function () {document.open();document.domain='localhost';document.close();})();" style="width:100%;height:100%;" seamless="seamless" scrolling="no" frameborder="0"></iframe>
In IFRAME we have to set the domain as same domain of the requesting window document. Else it will not work. Here I have set localhost.
JAVASCRIPT
$.support.cors = true;
var frameSize = 0;
$("#doc-frame").css({ 'height': frameSize, 'width': frameSize, 'zoom': '100%' });
$.ajax({
url: fileNamePath,// URL of the request page
context: document.body,
success: function (response) {
target = $("#doc-frame")[0].contentDocument;
target.open();
target.write(response);
target.close();
var frameMargin = 10;
$("#doc-frame").css("height", parseFloat($('#doc-frame').contents().height() + frameMargin) + "px");
$('#doc-frame').css("width", parseFloat($("#doc-frame").contents().width() + frameMargin) + "px");
}
});
It will work. Please use this code and please let me know the comments, drawbacks and limitations. I don't think this can fulfill all our cross domain IFRAME needs. But in some situations, we can use it.
Thank you