views:

238

answers:

5

I want to print a web page using javascript. But I do not want to open the page as a popup windows. How can I print directally a web page like 'mypage.aspx' using javascript window.print method without opening it as a popup window?

Also the condition is 'I dont want to use any Activex for this'

+4  A: 
<html>
    <head>
        <title>Print</title>
        <link rel="stylesheet" type="text/css" media="all" href="all.css" />
        <link rel="stylesheet" type="text/css" media="print" href="print.css" />
    </head>
<body>
    <p>I get printed</p>
    <form>
        <input type="button" onclick="window.print()" value="Print" />
    </form>
</body>
</html>

Make sure all.css is on top and print.css at the bottom, it should work.

Rosdi
I have done everything, please go through my question and comment first and then suggest some solution.
Rick
Gaurav, you're getting free help from a community of volunteers. Scolding them for answers that you don't like isn't a good way to encourage them to spend their time to help you...
sblom
@Gaurav, I rewrite the answer to include a full working html sample, hope it helps.
Rosdi
Hey guys, I am really sorry, if you feel my words are harsh, I do not want to push anyone for answer. I am just wandering for solution.
Rick
I think what Gaurav wants is the page to automatically print without user interaction at all?, call window.print() in body onLoad then.
Rosdi
To clarify the point of the print stylesheet, if you only want to print *part* of the page, put `display: none` rules in your `print.css` so that all other parts of the page are hidden. Then when you call normal `print()`, it will only print the non-hidden element.
bobince
+1  A: 

Um. Just use window.print(); directly on the page. By itself it does not open a new window (apart from the print properties window to set printing options).

Oded
+2  A: 

Not sure if it works, but you may try to create invisible iframe, load page2.aspx in it and then print it.

binaryLV
A: 

You could just use a CSS print style sheet and avoid using javascript altogether -all the user has to do them is print the page. e.g.

<link rel="stylesheet" type="text/css" media="print" href="print.css" />
matpol
+4  A: 

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>
jerjer