JS: window.open - 如何设置 Open Sans 字体?

JS: window.open - how to set Open Sans font?

我无法让它工作。我唯一需要的是 - 将所有内容字体系列设置为 'Open Sans'。这是我现在拥有的

        var popupWin = window.open('', '_blank', 'width="100%",height="100%"');
        popupWin.document.write('<html><head><link href=\'http://fonts.googleapis.com/css?family=Open+Sans\' rel=\'stylesheet\' type=\'text/css\'></head><body>' + content + '</body></html>');
        popupWin.document.body.setAttribute('style', 'font-family: \'Open Sans\' !important');
        popupWin.document.close();
        popupWin.print();
        popupWin.close();
    }

这段代码显示空页,但是当我删除 link 属性时,内容显示了,但使用了默认系列。有什么想法吗?

P.S。 content 变量包含一个简单的 html 标记,我试图将样式设置为

<div style="font-family='Open Sans'">

但运气不好。

http 更改为 https。您可能还想在打印前添加延迟。尝试以下操作:

document.querySelector('button').onclick = function() {
  var content = document.querySelector('textarea').innerHTML;

  var popupWin = window.open('', '_blank', 'width="100%",height="100%"');
  // Popup locked by browser
  if (!popupWin) {
    console.log('Popup was blocked');
    return false;
  }
  popupWin.document.write('<html><head><link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css"></head><body>' + content + '</body></html>');
  popupWin.document.body.setAttribute('style', 'font-family: "Open Sans" !important');
  setTimeout(function() {
    popupWin.document.close();
    popupWin.print();
    popupWin.close();
  }, 2000);
};
<h1>Test</h1>
<textarea>Put this on the popup page</textarea>
<button>Print</button>