RAVITEJA SATYAVADA
RAVITEJA SATYAVADA

Reputation: 2571

printing a page using javascript

please give me a solution to this. when a button is clicked I want to print a page specified in form action attribute using javascirpt:print() but without redirecting to that page

Upvotes: 0

Views: 516

Answers (2)

Andreas
Andreas

Reputation: 5335

You might want to consider the following...

You could load the page you want to print in an Iframe and print the contents of the iframe - without redirecting to that (target) page.

You can find more here:http://stackoverflow.com/questions/1261561/printing-a-hidden-iframe-in-ie

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1073968

You can't print a page the browser isn't showing. You can only ask the browser to show the print dialog, which will offer to print the page the browser is currently showing.

You could open a new window, though, and do it that way:

var form = document.getElementById("theForm"); // Assuming the form has `id="theForm"`
var wnd = window.open(form.action);
wnd.print();

If you control the other page, ideally you'd trigger the print call from within the other page when the page is fully loaded. On most browsers, the above works fine, but if I recall correctly on some browsers if the print call happens before the page is fully loaded, you don't quite get what you want...

Note that the code above will only work in direct response to a user action, such as a click on a button. Otherwise, the browser's pop-up blocker will prevent it happening.

Upvotes: 1

Related Questions