Created
February 10, 2010 08:48
-
-
Save provideal/300155 to your computer and use it in GitHub Desktop.
Printing an iFrame with jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$(function() { | |
$(".print").live('click', function(e) { | |
e.preventDefault(); | |
// remove old printframe | |
$("#printframe").remove(); | |
// create new printframe | |
var iFrame = $('<iframe></iframe>'); | |
iFrame | |
.attr("id", "printframe") | |
.attr("name", "printframe") | |
.attr("src", "about:blank") | |
.css("width", "0") | |
.css("height", "0") | |
.css("position", "absolute") | |
.css("left", "-9999px") | |
.appendTo($("body:first")); | |
// load printframe | |
var url = $(this).attr("href"); | |
if (iFrame != null && url != null) { | |
iFrame.attr('src', url); | |
iFrame.load(function() { | |
// nasty hack to be able to print the frame | |
var tempFrame = $('#printframe')[0]; | |
var tempFrameWindow = tempFrame.contentWindow? tempFrame.contentWindow : tempFrame.contentDocument.defaultView; | |
tempFrameWindow.focus(); | |
tempFrameWindow.print(); | |
}); | |
} | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note have to change:
$("#printframe").remove();
by
if($("#printframe"))
$("#printframe").remove();