-1

I want the contents of a link get printed by jQuery. What do i do?

Following is my code:
DEMO:

<a href="#" onclick="window.print(); return false;">Print</a> 
​
$(document).ready(function() {
    $('ul#tools').prepend('<li class="print"><a href="#print">Click me to print</a></li>');
    $('ul#tools li.print a').click(function() {
        window.open('www.google.com');
        window.print();
        return false;
    });
}); ​
jennifer Jolie
  • 727
  • 6
  • 16
  • 30

2 Answers2

0

"Printing the contents of a link" sounds ambiguous. I guess what you are trying to do is to print another web page? It might work if you opened the page in a new frame and printed that, it seems to me that this question might help you.

Community
  • 1
  • 1
ZeroOne
  • 3,041
  • 3
  • 31
  • 52
0

You would have to call print() on the window you are opening, not the one the original code is running in.

var foo = window.open(bar, 'bar');
foo.print();

(You might run into timing issues if the window hasn't had time to load the document).

However, in you example you appear to be trying to open a third party page (although you forgot the http://). The same origin policy will prevent you accessing the print method of the remote document.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Can it be used in my code? Please give an example here:http://jsfiddle.net/9zLGZ/ – jennifer Jolie Apr 22 '12 at 09:56
  • Sadly you can not use any functions/methods of a cross-origin frame. You are opening a different domain and javascript does not allow javascript from domain A to run in domain B. `$(window.open('http://www.google.com')).ready(function(){this.print();});` will cause a cross-domain error. (also, that piece of code is what you want to use) – Daan Timmer Apr 22 '12 at 12:46