7

i want to know if there is any cross-browser print code, that is if i need other then just simple:

//print page
    $('.print').click(function() {
        window.print();
        return false;
    });

i did found for bookmark and thats why i was more concern about print too, but couldn't find anything useful on google.

following code is for bookmark cross-browser

//bookmark page
$("a.bookmark").click(function(e)
{
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;

    if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
    } else if(window.opera) { // For Opera Browsers
        $("a.jQueryBookmark").attr("href",bookmarkUrl);
        $("a.jQueryBookmark").attr("title",bookmarkTitle);
        $("a.jQueryBookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
        alert('Your browser does not support this bookmark action');
        return false;
    }
});
Basit
  • 16,316
  • 31
  • 93
  • 154

4 Answers4

23

window.print() is a de-facto standard. (it's been supported since the days of IE4/Netscape 4).

While you're at it, be sure to check out how you can customize how your page looks when it's printed using print-specific CSS stylesheets.

broofa
  • 37,461
  • 11
  • 73
  • 73
  • 1
    `window.print()` invokes nothing on Chrome if you have popup blockers :/ – balexandre Sep 12 '13 at 08:29
  • 1
    @balexandre - link to bug, article, or test case documenting that behavior, please. If that's really the case, it would break a large number of websites that rely on `window.print` to implement their own print UI. :/ – broofa Sep 16 '13 at 18:47
  • 1
    just try yourself! using [AdBlockPlus](https://chrome.google.com/webstore/detail/adblock-plus/cfhdojbkjhnklbpkdaibdccddilifddb). – balexandre Sep 17 '13 at 06:38
  • @balexandre - `window.print()` works as expected with both AdBlock and AdBlockPlus extensions in chrome. Perhaps this is just an issue for you? (Hence my request for some supporting documentation of the problem) – broofa Sep 18 '13 at 00:49
  • maybe it is just for me, even with the new Chrome v.29 I have the issue :( – balexandre Sep 19 '13 at 09:37
  • It's not part of a standard. See http://stackoverflow.com/a/9269238/3150057. But I see what you mean by "de-facto" as in "it's existed for a long, long time and is used pretty much everywhere." – Henry Blyth May 21 '14 at 10:16
2

window.print() will do the job.

Marko
  • 71,361
  • 28
  • 124
  • 158
0

First you need to set your html to have Content Security Policy similar to this:

frame-src * 'self' blob: data:

Example:

<meta http-equiv="Content-Security-Policy" content="default-src * 'self' 'unsafe-inline' 'unsafe-eval' data: gap: content:; frame-src * 'self' blob: data:">

The important part here is: frame-src blob: data: (the blob part)

Then, you can print using a "local" non cross-domain way by setting the iframe to a load a blob instead of an "outside URL"

Using this:

 function printPage(url) {
      fetch(url)
        .then(function (response) {
          return response.blob();
        })
        .then(function (myBlob) {
          var newFrame = document.createElement('iframe');
          var existingDOMIframe = document.getElementById('printIframe');
          if (existingDOMIframe) {
            document.body.removeChild(existingDOMIframe);
          }

          newFrame.id = 'printIframe';
          newFrame.style.width = '0';
          newFrame.style.border = '0';
          newFrame.style.height = '0';
          newFrame.style.position = 'relative';
          var objectURL = URL.createObjectURL(myBlob);
          newFrame.src = objectURL;
          newFrame.onload = function () {
            document.querySelector('#printIframe').contentWindow.print();
          };
          document.body.appendChild(newFrame);
          objectURL = URL.revokeObjectURL(myBlob);
        });
    }
Miguel
  • 3,349
  • 2
  • 32
  • 28
0

That is the general way. It is not an official part of the dom. I would check for its existence first.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445