jquery - odmowa dostępu do iframe w IE na niektórych stronach
Jestem autorem printThis, wtyczki jquery do drukowania.
https://github.com/jasonday/printThis
Mam użytkownika, który zgłosił problem, którego nie mogłem złamać i niestety nie mogę udostępnić strony (obawy dotyczące prywatności).
Na stronie użytkownika problem pojawia się na niektórych stronach w IE, ale nie na innych. Wydruk się nie udaje, ponieważ ramka pozostaje pusta.
Błąd w IE jest w jQuery:
contents: function (a) {
return f.nodeName(a,
"iframe") ? a.contentDocument || a.contentWindow.document : f.makeArray(a.childNodes)
}
Korzystając z rejestrowania, udało mi się ustalić, że zawiodła ta linia:
var $doc = $("#" + strFrameName).contents();
Ale znowu dzieje się to tylko na niektórych stronach i nie byłem w stanie odtworzyć w żadnym przypadku poza witryną tego użytkownika.
Moje pytanie: czy jest tu lepsze podejście? lub metoda wykonania$doc
obiekt bardziej kuloodporny?
// -----------------------------------------------------------------------
// printThis v1.1
// Printing plug-in for jQuery
//
// Resources (based on) :
// jPrintArea: http://plugins.jquery.com/project/jPrintArea
// jqPrint: https://github.com/permanenttourist/jquery.jqprint
// Ben Nadal: http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
//
// Dual licensed under the MIT and GPL licenses:
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
//
// (c) Jason Day 2012
//
// Usage:
//
// $("#mySelector").printThis({
// debug: false, //show the iframe for debugging
// importCSS: true, // import page CSS
// printContainer: true, // grab outer container as well as the contents of the selector
// loadCSS: "path/to/my.css" //path to additional css file
// });
//
// Notes:
// - the loadCSS option does not need @media print
//------------------------------------------------------------------------
(function($) {
var opt;
$.fn.printThis = function (options) {
opt = $.extend({}, $.fn.printThis.defaults, options);
var $element = (this instanceof jQuery) ? this : $(this);
// if Opera, open a new tab
if ($.browser.opera)
{
var tab = window.open("","Print Preview");
tab.document.open();
}
// add dynamic iframe to DOM
else
{
var strFrameName = ("printThis-" + (new Date()).getTime());
var $iframe = $("<iframe id='" + strFrameName +"' src='about:blank'/>");
if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
$iframe.appendTo("body");
}
// allow iframe to fully render before action
setTimeout ( function () {
if ($.browser.opera)
{
var $doc = tab.document;
} else
{
var $doc = $("#" + strFrameName).contents();
}
// import page css
if (opt.importCSS)
{
$("link[rel=stylesheet]").each(function(){
var href = $(this).attr('href');
if(href){
var media = $(this).attr('media') || 'all';
$doc.find("head").append("<link type='text/css' rel='stylesheet' href='" + href + "' media='"+media+"'>");
}
});
}
// add another stylesheet
if (opt.loadCSS)
{
$doc.find("head").append("<link type='text/css' rel='stylesheet' href='" + opt.loadCSS + "'>");
}
//add title of the page
if (opt.titlePage)
{
$doc.find("head").append('<title>'+opt.titlePage+'</title>');
}
//grab outer container
if (opt.printContainer) { $doc.find("body").append($element.outer()); }
else { $element.each( function() { $doc.find("body").append($(this).html()); }); }
//$doc.close();
// print
($.browser.opera ? tab : $iframe[0].contentWindow).focus();
setTimeout( function() { ($.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
//removed iframe after 60 seconds
setTimeout(
function(){
$iframe.remove();
},
(60 * 1000)
);
}, 333 );
}
$.fn.printThis.defaults = {
debug: false, //show the iframe for debugging
importCSS: true, // import page CSS
printContainer: true, // grab outer container as well as the contents of the selector
loadCSS: "", //path to additional css file
titlePage: "" //add title to print page
};
jQuery.fn.outer = function() {
return $($('<div></div>').html(this.clone())).html();
}
})(jQuery);
AKTUALIZACJA
Problem z powodudocument.domain
Ten typ strony madocument.domain
zestaw i IE nie dziedziczydocument.domain
od rodzica.
Aby naprawić tę część, zmieniłem tworzenie iframe na standardowy javascript i ustawiłem źródło do zapisudocument.domain
na tworzenie iframe.
var printI= document.createElement('iframe');
printI.name = "printIframe";
printI.id = strFrameName;
document.body.appendChild(printI);
printI.src = "javascript:document.write('<head><script>document.domain=\"mydomain.com\";</script></head><body></body>')";
var $iframe = $("#" + strFrameName);
To naprawia odmowę dostępu, jednak teraz ramka nie będzie drukowana. Próbowałem wielu różnych metod dostępu do obiektu, jednak żaden z nich nie działa.
A) w jaki sposób uzyskasz dostęp do ramki w tym scenariuszu (wypróbowałem większość metod przedstawionych na SO), aby IE rozpoznał i wydrukował
lub
B) Czy ktoś może pomyśleć o lepszym sposobie uzyskania dokumentu.domain w iframe podczas tworzenia za pomocą jQuery? (nie może być później, ponieważ pojawi się problem z odmową dostępu)