0

My previous colleague rewrite window.print method:

function print(data){

    var window_print = window.open('', 'my div', 'height=768, width=1024');

    window_print.document.write('<!DOCTYPE html><html><head><title>Печать</title></head><body>' + data + '</body></html>');

    window_print.print();
    window_print.close();
}

My intention was to use default behavior of that function: just print current page, and I added:

if(data) {....} else { window.print() }

And of course I received error: "too much recursion: window.print();"

My question is how to invoke default behavior window.print()?

2 Answers2

1

Edit: Ok, it appears that print is an own property of window in some browsers, and isn't in others. Therefore, just cache the value of window.print:

var printWindow = window.print;

// define your new print function here
var print = function(data) { ... };

// then later:
printWindow.call(window);

NB: If you're doing all this in the global scope, then you'll need to define the new print using a function expression (var print = ...) rather than a function declaration (function print(data) { ... }) because function declarations get hoisted to the top of their scope, and therefore the print redefinition would happen before you had chance to cache it. If you're not doing it in the global scope, then it doesn't matter as the new print won't override window.print regardless of how it's defined.

Original:

Try:

Object.getPrototypeOf(window).print.call(window);

print doesn't appear to be an own property of window, meaning that the newly defined print merely shadows something further up the prototype chain. You can bypass this shadowing by moving up the prototype chain using Object.getPrototypeOf.

You'll also need to use call so that the print method receives the correct value for this.

Ben Jackson
  • 11,722
  • 6
  • 32
  • 42
  • @yonishepelev ah, it appears like `print` is an own property of window in `firefox`. Looks like you'll have to cache the value of `print` before defining the new value, and use the cached reference. – Ben Jackson Oct 08 '14 at 21:00
  • @BYossarian unfortunately your edit suffers the same as my original answer. – Rhumborl Oct 09 '14 at 08:18
0

You need to store the original print method as another property in the window, just before the definition of your own print().

EDIT: You also need to define the new print function specifically as window.print = function(){...} rather than function print(){...} in order to be able to access the original - see answers with nice links here and here. This won't have any impact on how you call the method.

window.originalPrint = window.print;

window.print = function(data)
{
    if(data)
    {
        document.getElementById('foo').innerHTML = data;
    }
    else
    {
        window.originalPrint();
    }
}
<div id="foo"></div>

<button onclick="window.print('hello')">print('hello')</button>
<button onclick="window.print()">print()</button>
Community
  • 1
  • 1
Rhumborl
  • 16,349
  • 4
  • 39
  • 45