Printing a web page using just url and without opening new window?
You can do this using a hidden iFrame (I'm using jquery for the example):
function loadOtherPage() {
$("<iframe>") // create a new iframe element
.hide() // make it invisible
.attr("src", "/url/to/page/to/print") // point the iframe to the page you want to print
.appendTo("body"); // add iframe to the DOM to cause it to load the page
}
This will load the page you want to print. To print, you can add javascript code to the print page so that it gets printed after loading:$(document).ready(function () {
window.print();
});
This will print the page without showing a new window. I've tested this in IE8,9 and Google Chrome, so I'm not sure if this works for Safari or Firefox, though. How to print a web page without opening a popup window?
The simpliest solution is to load the content of that mypage.aspx to an iframe then on iframes onload event call the window.print.
<button onclick="printPage()">print</button>
<div id="printerDiv" style="display:none"></div>
<script>
function printPage()
{
var div = document.getElementById("printerDiv");
div.innerHTML = '<iframe src="mypage.aspx" onload="this.contentWindow.print();"></iframe>';
}
</script>
Print external page without open it
Create an iframe, hide it, and then call the proper print functions. The execCommand should work for all versions of IE.
Mind you: $.browser won't work for newer versions of jQuery and should be avoided. Use your preferred way of detecting features.
var ifr = createIframe();
ifr.hide();
if ($.browser.msie) {
ifr.contentWindow.document.execCommand('print', false, null);
} else {
ifr.contentWindow.focus();
ifr.contentWindow.print();
}
This was developed for IE, FF and Chrome. I have no idea how well this will work for Safari and Opera, but it might give you some ideas.Edit: as adeneo correctly pointed out, $.browser is deprecated and should be avoided. I updated my statement. I'll leave my code untouched, as it still expresses the correct intent.
Related Topics
Loading Local Files with JavaScript Without a Web Server
How to Add My Own Methods to HTMLelement Object
How to Execute a Dynamically Loaded JavaScript Block
JavaScript Getelementbyname Doesn't Work
How to Pull the File Name from a Url Using JavaScript/Jquery
How to Check "Checkbox" Dynamically - Jquery Mobile
How to Check If an Array Is a Subset of Another Array in JavaScript
Adding Csrftoken to Ajax Request
Arrow VS Classic Method in Es6 Class
How to Insert Variables in JavaScript Strings
How to Unbind a Listener That Is Calling Event.Preventdefault() (Using Jquery)
Click Everywhere But Here Event
How to Adapt Trampolines to Continuation Passing Style
How Do Print the Console Output of the Page in Puppeter as It Would Appear in the Browser
How to Sign into Google with Selenium Automation Because of "This Browser or App May Not Be Secure."