admin管理员组

文章数量:1332345

I have the following js code:

 window.print();

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

The following does work in 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:

 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;

I have the following js code:

 window.print();

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

The following does work in 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:

 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;
Share Improve this question edited Sep 12, 2013 at 7:35 user984003 asked Sep 12, 2013 at 5:39 user984003user984003 29.6k69 gold badges202 silver badges315 bronze badges 8
  • 2 have you checked...popups are blocked or not...??? – guri Commented Sep 12, 2013 at 5:42
  • where are you calling window.print(); ..? – Sudhir Bastakoti Commented Sep 12, 2013 at 5:49
  • It's called in my js code. It's NOT inside a onClick. The page loads, and then it's called. – user984003 Commented Sep 12, 2013 at 5:52
  • 1 If pop-ups were blocked then I don't see how it would work when I include the alert() – user984003 Commented Sep 12, 2013 at 5:52
  • Can you show the whole loadJS function? window.print() works fine here, testing it in the dev console on FF-23.0.1. – Joe Simmons Commented Sep 12, 2013 at 6:22
 |  Show 3 more ments

1 Answer 1

Reset to default 4

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

本文标签: javascriptwindowprint() not working in FirefoxStack Overflow