3

If the user tries to print (using ctrl+p) without checking at least one checkbox on the page an error message gets prompted. On IE 11, I do see the error message but simultaneously the print window (popup) also appears. How can I prevent the print window?

$(window).bind('keydown', function (event) {

  if (event.ctrlKey || event.metaKey) {
    switch (String.fromCharCode(event.which).toLowerCase()) {
      case 'p':
        event.preventDefault();
        //alert('ctrl+p');
        printFunc();
        break;

      }
    }
});

Also tried :

window.onbeforeprint = function () {
    //alert('ctrl-p');
    printFunc();
    event.preventDefault();
    return false;
};

Function:

function printFunc() {
    var selectedListinsCount = selected_Listings.join('').split('').length;
    if (selectedListinsCount < 1) {
          if ($('#errmesg').length == 0) {
            $('.messageCenter').append('<span id="errmesg" class ="errmesg"> <span class="messageIcon"></span><span>Please select at least one listing</span></span>');
          }
        $('.messageCenter').show();
        return false;
    }
    else {
        $('.errmesg').remove();
        $('.messageCenter').hide();
    }

}
David Mulder
  • 26,123
  • 9
  • 51
  • 114
BumbleBee
  • 10,429
  • 20
  • 78
  • 123

2 Answers2

0

I would agree that inhibiting browsers' OS-level functionality is probably not the best approach, and becoming less possible over time- essentially if you did get this working, it'd be a bit of a hack that would lack sustainability.

Consider changing your flow to incorporate validation BEFORE presenting the data you want the user to print.

Mark Mutti
  • 86
  • 4
0

You should use return; instead of return false;.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Abhijit Pandya
  • 705
  • 2
  • 12
  • 33