2

I need to open some url in new window as a print window. I have option to open a new window with window.open(url); but I need to open it as Print window, not normal window

Kavi
  • 120
  • 1
  • 9
  • Need to open a new tab with google first page as print content – Kavi Mar 08 '18 at 10:27
  • 2
    Possible duplicate of [SecurityError: Blocked a frame with origin from accessing a cross-origin frame](https://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame) and [Javascript Print iframe contents only](https://stackoverflow.com/questions/9616426/javascript-print-iframe-contents-only) – arhak Mar 08 '18 at 10:35
  • or i can give the url directly - how to print it in a new window? – Kavi Mar 08 '18 at 10:38
  • Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Mar 08 '18 at 11:33

3 Answers3

3
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $("#testbtn").click(function () {
            var newWin = window.frames[0];
            newWin.document.write('<body onload="window.print()"><iframe style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;" src="https://www.mantisbt.org"></body>');
            newWin.document.close();
        });
    });
</script>
</head>
<body>
<div id="divPaySlip">
    <button type="button" id="testbtn">Print</button>
    <iframe id="ifrPaySlip"  name="ifrPaySlip" scrolling="yes" style="display:none"></iframe>

</div>
</body>
</html>

Got the answer after lot of searches. https://www.mantisbt.org is the url which I want to print in a new tab.(fake url)

Kavi
  • 120
  • 1
  • 9
2

For more customization you can do something like:

var printWindow = window.open(
    'www.google.com', 
    'Print', 
    'left=200', 
    'top=200', 
    'width=950', 
    'height=500', 
    'toolbar=0', 
    'resizable=0'
);
printWindow.addEventListener('load', function() {
    printWindow.print();
    printWindow.close();
}, true);
Jeffrey Roosendaal
  • 5,961
  • 8
  • 33
  • 50
hazrat
  • 71
  • 5
  • Wrap the above code in a function and call it on a button click where ever you need etc – hazrat Mar 08 '18 at 11:12
  • yes if you need it like that way I just give a hint – hazrat Mar 08 '18 at 11:16
  • it is opening a window with content of the url. not the print page. Need the equivalent result of window.print(); – Kavi Mar 08 '18 at 11:18
  • A bit strange that load event is not triggering , looking into it – hazrat Mar 08 '18 at 12:12
  • working example: https://jsfiddle.net/0ohz1cn7/ however, notice the limitation depicted in https://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame – arhak Mar 08 '18 at 12:21
  • I am adding a note here: Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script block finishes executing. The window creation and the loading of the referenced resource are done asynchronously. so we can't add an Event Listener to it because the printWindow object returns is blank , This is what I found on Mozilla documentation – hazrat Mar 09 '18 at 06:49
0

I followed Kavi Yarasan's Solution
but it didn't seem to work for me.
Then I tried below code and it worked like a charm.

   $.ajax({
      url: my_url,
      type: "GET",
      success: function (data) {
         newWin= window.open("");
         newWin.document.write(data);
         newWin.print();
         newWin.close();
      }
    });
Dharman
  • 21,838
  • 18
  • 57
  • 107
Muhammad Bilal
  • 385
  • 3
  • 12