onbeforeprint() and onafterprint() equivalent for non IE browsers

后端 未结 4 1867
慢半拍i
慢半拍i 2020-11-28 12:52

I want to send some info back to my database when a user prints a certain web page. I can do this in IE with onbeforeprint() and onafterprint() bu

4条回答
  •  臣服心动
    2020-11-28 13:27

    Try masking the native window.print() with your own...

    // hide our vars from the global scope
    (function(){
    
      // make a copy of the native window.print
      var _print = this.print;
    
      // create a new window.print
      this.print = function () {
        // if `onbeforeprint` exists, call it.
        if (this.onbeforeprint) onbeforeprint(this); 
        // call the original `window.print`.
        _print(); 
        // if `onafterprint` exists, call it.
        if (this.onafterprint) onafterprint(this);
      }
    
    }())
    

    Updated: comments.

提交回复
热议问题