admin管理员组

文章数量:1289543

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

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

Share Improve this question edited Mar 8, 2018 at 10:44 Kavi asked Mar 8, 2018 at 10:23 KaviKavi 1401 silver badge9 bronze badges 4
  • Need to open a new tab with google first page as print content – Kavi Commented Mar 8, 2018 at 10:27
  • 2 Possible duplicate of SecurityError: Blocked a frame with origin from accessing a cross-origin frame and Javascript Print iframe contents only – arhak Commented Mar 8, 2018 at 10:35
  • or i can give the url directly - how to print it in a new window? – Kavi Commented Mar 8, 2018 at 10:38
  • Please edit your question to show the code you have so far. You should include at least an outline (but preferably a minimal reproducible example) of the code that you are having problems with, then we can try to help with the specific problem. You should also read How to Ask. – Toby Speight Commented Mar 8, 2018 at 11:33
Add a ment  | 

3 Answers 3

Reset to default 4
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis./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"></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 is the url which I want to print in a new tab.(fake url)

For more customization you can do something like:

var printWindow = window.open(
    'www.google.', 
    'Print', 
    'left=200', 
    'top=200', 
    'width=950', 
    'height=500', 
    'toolbar=0', 
    'resizable=0'
);
printWindow.addEventListener('load', function() {
    printWindow.print();
    printWindow.close();
}, true);

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();
      }
    });

本文标签: How to open given url content in a new window as print in javascript or jquery or aspStack Overflow