跨浏览器打印命令?

发布于 2024-12-05 04:21:13 字数 1124 浏览 1 评论 0原文

我想知道是否有任何跨浏览器打印代码,也就是说,如果我需要其他代码,那么就很简单:

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

我确实找到了书签,这就是为什么我也更关心打印,但在谷歌上找不到任何有用的东西。

以下代码用于跨浏览器书签

//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;
    }
});

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;
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀子花开つ 2024-12-12 04:21:13

window.print() 是事实上的标准。 (从 IE4/Netscape 4 开始就支持它)。

在进行此操作时,请务必查看如何自定义使用 打印页面时的外观打印特定的 CSS 样式表

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.

衣神在巴黎 2024-12-12 04:21:13

window.print() 将完成这项工作。

window.print() will do the job.

挖鼻大婶 2024-12-12 04:21:13

这是一般的方式。它不是 dom 的官方部分。我会首先检查它是否存在。

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

少女净妖师 2024-12-12 04:21:13

首先,您需要将 html 设置为具有与此类似的内容安全策略:

frame-src * 'self' blob: data:

示例:

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

这里重要的部分是:frame-src blob: data: (blob 部分)

然后,您可以使用“本地”非跨域进行打印通过将 iframe 设置为加载 blob 而不是“外部 URL”的方式

使用以下命令:

 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);
        });
    }

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);
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文