I'm trying to print another page on my domain by passing window.open the url, and using window.focus, window.print to print. However, the print preview shows only an empty page. I'm guessing I have to wait until the page is loaded, but I'm not sure exactly how to do this. Here's the code:
var newWin=window.open('http://mydomain.com');
newWin.focus();
newWin.print();
newWin.close();
I've tried stuff like
newWin.onload() {
newWin.print();
});
to no avail.
Edit 1:
var newWin=window.open('http://localhost:76');
newWin.focus();
newWin.onload = newWin.print();
newWin.close();
Same problem persists
Edit 2:
var newWin=window.open('http://localhost:76');
newWin.focus();
newWin.body.onload = newWin.print();
Adding newWin.close() here causes the print function to
bug out and only print the
title of the page. Otherwise, the page is printing properly with this
Edit 3:
function printWin(newWin) {
newWin.print();
newWin.close();
}
var newWin = window.open('http://localhost:76');
newWin.focus();
newWin.body.onload = printWin(newWin);
This causes the print to happen prematurely like before, previewing an empty page. wtf :(
Open the popup. Then check if the popup is ready. When it's ready, inject a javascript.
<script type="text/javascript">
var popup = window.open("b.htm");
var body;
function check() {
body = popup.document.getElementsByTagName("body");
if (body[0] == null) {
setTimeout(check, 50);
} else {
var n = popup.document.createElement("script");
n.src = "printandclose.js";
body.appendChild(n);
}
}
check();
</script>
printandclose.js
window.print();
window.open("", "_self");
window.close();
Let me know if it works. If you don't add window.open("", "_self"); a alert will popup telling the user that the window is about to close.
Related
I am trying to call openswf function when i click on SWF part1 links which is in document.write(), but my problem is when i clicked on SWF part1 links it nothing happen.
This is my code.
<html>
show popup
<script>
function openswf(){
console.log("Do something")
}
function Popup()
{
var win = window.open('', '',"toolbar=no, width=1000, height=800");
var doc = win.document.open();
doc.write('SWF part1');
doc.close();
}
</script>
</html>
First i click on show popup links to call Popup() function and open new popup window which is work properly and then i want to click on SWF part1 links in that popup but it not working.
openswf is not defined in the popup window, but in the main window. But you can reference the main window with opener. I suppose you want to close the popup window when the user clicks:
doc.write('SWF part1');
It's probably calling the default <a> onClick event.
You have to preventDefault().
function Popup(event)
{
event.preventDefault();
var win = window.open('', '',"toolbar=no, width=1000, height=800");
var doc = win.document.open();
doc.write('SWF part1');
doc.close();
}
Let me know if it helps.
For starters, the variable "path" is not being passed to your new window. Use this instead...
function Popup(path)
{
var win = window.open('', '',"toolbar=no, width=1000, height=800");
var doc = win.document.open();
doc.write('SWF part1');
doc.close();
}
I am using the below function in order to print a target Div using JS. This works fine in all browsers apart from chrome. The console throws the error: PrintElem is undefined.
function PrintElem(elem) {
Popup($(elem).html());
}
function Popup(data) {
var mywindow = window.open('', 'to_print', 'height=600,width=800');
var html = '<html><head><title></title>' +
'</head><body onload="window.focus(); window.print(); window.close()">' +
data +
'</body></html>';
mywindow.document.write(html);
mywindow.document.close();
return true;
}
I have tried calling the function onclient click with the following:
PrintElement('[id$=divExport]');
PrintElement('divExport');
PrintElement('#divExport');
PrintElement('#ctl00_body_ucJobDetails_divExport');
PrintElement('ctl00_body_ucJobDetails_divExport');
It works in Firefox and incredibly in IE 9 and 11!
Any advice would be greatly appreciated.
Your function name is PrintElem(). But you are calling it as PrintElement(). Apart from this I don't see any other problem..
And use this line.
PrintElem('#divExport');
This is a followup to a prior question that you guys answered a few weeks ago. There is HTML in a WordPress.org app which I cannot change. A section in this HTML contains text that is hyperlinked to someURL. I want the URL to redirect to my referrer page in order to return to whatever page I cam from when I click on this link. The problem is that something is blocking the assignment of my referrer URL string to HREF. I stepped through this function a hundred times in the Chrome debugger, and tried various thing (see below). HREF simply will not change, not matter what. Any idea why? here is the code, and thank you for any any help on this:
window.onload = function () {
document.querySelector(".button.wc-backward").onclick = function() {
var URLstring = document.referrer; // works fine
window.location.href = URLstring; // ref unchanged
setTimeout(function () {
window.location.href = URLstring; },100); // same result
return false;
}
}
There is no issue with you jscode, I have added it to here:
Example on github
And it is working. (You will be redirected back to stackoverflow).
<html>
<head>
</head>
<body>
<script>
window.onload = function () {
document.querySelector(".button.wc-backward").onclick = function() {
var URLstring = document.referrer; // works fine
window.location.href = URLstring; // ref unchanged
setTimeout(function () {
window.location.href = URLstring;
}, 100); // same result
return false;
}
}
</script>
the link
</html>
You code is wokring... do you like to try this one too
window.location.assign(URLstring)
Totally noob question over here, I have this JS code where using only ONE function works perfectly, but adding a second one makes it stop working.
function PSN() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/psn.html", "Fran", "width=380, height=400");
}
function BLIZ() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/bliz.html", "Fran", "width=380, height=400");
}
function XLA() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/xla.html", "Fran", "width=380, height=400");
}
function STEAM() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/steam.html", "Fran", "width=380, height=400");
}
PSN button works ok when there's only one function written, but as I put 2 or more, all of the links stop working.
Is it because of the syntax of this file? Or am I missing something?
you don't need multiple function to open pop up, you can just create single function and pass the url as param
function popUp(url) {
return window.open(url, "Fran", "width=380, height=400");
}
Note: Put your functions block in <header> of page then try. here is working fiddle
Update : Since all window names are same Fran you will need to close the opened window first before opening other. currently it will open the url in previously opened window.
function CallPrint() {
var prtContent = document.getElementById('<%= pnlDelete.ClientID %>');
var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
winPrint.document.write("<h3>Summary</h3><br />" + prtContent.innerHTML);
winPrint.document.close();
winPrint.focus();
winPrint.print();
winPrint.close();
}
I have a need where I have to print contents of a div. I am using above code to do so. It is working fine in IE but does nothing in Firefox. Am I missing something here that needs to be done in Firefox?
Instead of opening a new window without any URL, I opened this page in the window and accessed the contents of the pnlSummary from the opened window via window.opener object –
function CallPrint() {
var winPrint = window.open('Print.aspx', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
}
On Print.aspx page I used this function –
function Print() {
var prtContent = "<h3>Summary</h3>" + window.opener.document.getElementById('ctl00_cphContent_pnlSummary').innerHTML;
document.getElementById("printDiv").innerHTML = prtContent;
window.print();
window.opener.focus();
window.close(); }
and called it on body onload.
<body onload="Print();">
<form id="form1" runat="server">
<div id="printDiv">
</div>
</form>
</body>
This is working fine in both IE and Firefox.
Use setTimeout() function for loading the page. The example is given bellow link.
http://oraclehappy2help.blogspot.in/2012/09/child-window-printing-problem-solution.html
Uhm... your code seems to work fine for me, on Firefox 3.5 (Windows).
It's possible that are something wrong on your pnlDelete.ClientID?
Your javascript code is rendered well on the page?
Anyway I suggest you to use jQuery + a print plugin like this.
Check to ensure your panel has something. My guess is prtContent is undefined
Try this:
function CallPrint() {
var prtContent = document.getElementById('<%= pnlDelete.ClientID %>');
if (prtContent) {
var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
winPrint.document.write("<h3>Summary</h3><br />" + prtContent.innerHTML);
winPrint.document.close();
winPrint.focus();
winPrint.print();
winPrint.close();
}
else {
alert('No summary available for printing');
}
}
you can use JS Printer Setup https://addons.mozilla.org/en-us/firefox/addon/js-print-setup/"
which is Fire fox Depended addon most usefulladdon In web-app Kisok in Firefox to select printer
attached some example for attached printer and local printer it may help you to build without print dialog.
function EB_Print(printType) {
try{
var printerType = printType; // type of the Print Code : network
// Default Printer Configuring
var Default_printer = "Canon MG2500 series";
/** local Printer configuring via Network
** Config teh Local server use \\\\ to get \\
**/
var Organizer_Printer = "\\\\network\\Canon LBP2900";
jsPrintSetup.setPrinter(Default_printer);
jsPrintSetup.setSilentPrint(true);// withoud dialog
/** alert(jsPrintSetup.getPrintersList()); // Debugger for the attached Printers list
alert(jsPrintSetup.getPrinter()); // get the set printer Option
**/
// id network is selected It will print the page in network
if(printerType == 'network'){
jsPrintSetup.setPrinter(Organizer_Printer);
}
jsPrintSetup.print(); // Print the page
}catch (e) {
// TODO: handle exception
}
}
you could try a jquery plugin...
http://plugins.jquery.com/project/PrintArea