javascript window.print() 在 Firefox 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18756153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:06:52  来源:igfitidea点击:

window.print() not working in Firefox

javascript

提问by user984003

I have the following js code:

我有以下js代码:

 window.print();

This works in Chrome and IE. It also works on iPhone safari. However, it does not work on Firefox.

这适用于 Chrome 和 IE。它也适用于 iPhone Safari。但是,它不适用于 Firefox。

The following does work in Firefox

以下在 Firefox 中有效

alert()
window.print();

so I figure it's some kind of timing thing? Note that my js code only loads after the page itself has been loaded:

所以我认为这是某种时间安排?请注意,我的 js 代码仅在页面本身加载后才加载:

 function loadJS() {     
      var element = document.createElement("script"); // 
      element.src = "url/js/all.js";
      document.body.appendChild(element);    
 }

if (window.addEventListener)
      window.addEventListener("load", loadJS, false);
else if (window.attachEvent)
    window.attachEvent("onload", loadJS);
else 
   window.onload = loadJS;

回答by user984003

Well, just making it wait, worked, although it seems silly to have to do it. Plus, I don't know if this will always work...

好吧,只是让它等待,奏效了,尽管不得不这样做似乎很愚蠢。另外,我不知道这是否总是有效......

 setTimeout( 
    function() {
        window.print();
    }, 100);