How does the Javascript print function work? Can I create a document using javascript and print it off?

后端 未结 8 1679
梦毁少年i
梦毁少年i 2021-02-09 07:17

I know you can use window.print() to print the current page... but what I want to know is can I build a document using javascript in order to populate it with data and print it

8条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 07:46

    print() essentially just calls up the native print dialog for a given window.

    But as you're are thinking, it will work on any window or (i)frame.

    thus if you write content to a frame, you can then call this to print it.

    window.frameName.print();
    

    note the only drawback (and its a big one), is that this calls up the print dialog... not the print preview window... thus the user doesn't really get a chance to see what they are printing and/or scale it to fit their printer/paper.

    I personally wish that all browsers would implement the following to handle the above issue. ;-)

    window.printPreview();
    

提交回复
热议问题