Meher Krishna
Meher Krishna

Reputation: 113

Print a dynamically generated page using Javascript

Let me keep this simple and straight..

  1. We have a print button in PAGE1

  2. We have a PAGE2 which have data generated using some json and other stuff.

  3. We want to print PAGE2 when I click on a button in PAGE1. (Using Javascript/Jquery)

Please give an idea to attain this functionality.

Thank you.

Upvotes: 0

Views: 310

Answers (1)

Ralph Ritoch
Ralph Ritoch

Reputation: 3440

To print a page that isn't showing you need to use an old hack, hidden iframes.

If you place an "onload" call to window.print() on the page you want to print you can then simply have your button trigger a hidden iframe (1x1) to load that page. Your browser will block any XSS attempt so the javascript to cause a print operation needs to be on the page that is loaded.

Alternatively the child page can usually also access the parent page (as long as they are on the same domain) so you could also have the hidden child page available the entire time (but hidden) and have it register a click event listener on the parent page.

Upvotes: 1

Related Questions