After window.open, can't print in Safari for Mac

瘦欲@ 提交于 2019-12-13 21:10:31

问题


I create a secondary browser window with Javascript code, using the window.open function, and fill it programmatically with some HTML content. It works well for all browsers that my application supports except for one: Safari on Mac. In fact, the window itself is OK but the print command is disabled. Does anybody have an idea why? I should mention that the main reason to show this window is to allow the users to print some data. I guess I could implement a "Print" button in the page but I would prefer not to (and it may not work either, but I haven't tried it).

Here is a simplified example of the code that I use to create the HTML content:

        var pp = window.open("", "_blank");
        pp.document.writeln("<html>");
        pp.document.writeln("<head>");
        pp.document.writeln("<title>");
        pp.document.writeln("Hello");
        pp.document.writeln("</title>");
        pp.document.writeln("</head>");
        pp.document.writeln("<body>");
        pp.document.writeln("The body");
        pp.document.writeln("</html>");
        pp.document.close();

I tried variations around that code, without any success. My tests are done with Safari 5.1 on Mac OS X 10.6.8. Any help is welcome!


回答1:


Have the window print itself:

Before your </html> add:

pp.document.writeln("<script type='text/javascript'>window.print()</script>");


来源:https://stackoverflow.com/questions/9167768/after-window-open-cant-print-in-safari-for-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!