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
2 Answers
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.