views:

80

answers:

1

Hello,

I create a new iframe, append it to body, and then try to get its 'document' variable, like this:

var $iframe = $( "<iframe name='my-frame'>" );
$iframe.appendTo( $("body") );

var doc = null;

if($.browser.msie){
    doc = window.frames["my-frame"].document; // 'access denied' in IE
} else {
    doc = $iframe[0].contentWindow.document;
}
doc.close();

This works fine standalone, but once I try to do it from within a jQuery plugin, IE gives me 'Access is denied'.

(function($) {
    $.fn.jqprint = function (options) {

 // the above code fails in IE with above error when inserted here

    }
})(jQuery);

The weird thing is both ways work in IE on their own, browser check was inserted for debugging.

I'm guessing it's some sort of a scope problem, with 'document' not being accessible from within an anonymous function or something like this. Any pointers?

+1  A: 

I can't reproduce this here. Both ways work fine in IE6 and IE7 here with jquery 1.4.2 and 1.4.3, even without the browser check.

HTH

Side note:
For some reason [only on Linux, only IE7, IE6 is fine!], as soon as I added e.g. doc.write() (doc.open(),...) anywhere, even after some setTimeout, about every second time I reload the page the iframe does not appear on the page to begin with. Then $iframe[0] was still an object, but $iframe[0].contentWindow was undefined (also .readyState, .src, ...) leading to an "Unspecified Error" with the above code in either variant, even without jquery involved and a static iframe. Not reproducible on Windows, though.

smilingthax