简体   繁体   English

从javascript打印

[英]Printing from javascript

I have a page that displays search results. 我有一个页面显示搜索结果。 I'm looking for a way to have those search results printed. 我正在寻找一种打印这些搜索结果的方法。 However, there are some items (ads, menus, etc) that should not be printed. 但是,有些项目(广告,菜单等)不应打印。

What I have so far is the results are sent to a new tab (using javascript) with the search results. 到目前为止,我将搜索结果与搜索结果一起发送到新标签页(使用javascript)。 All of the items that shouldn't be visible have been removed. 所有不可见的项目均已删除。 However, only the text shows up. 但是,仅显示文本。 All the CSS styles and images aren't showing up. 没有显示所有CSS样式和图像。

Since the results are already being displayed, I'm trying to avoid having the server execute the search again. 由于结果已经显示,因此我试图避免让服务器再次执行搜索。

Another thing to keep in mind, this is an SPA site(ie the URL never changes throughout the site). 要记住的另一件事是,这是一个SPA网站(即,URL在整个网站中都不会改变)。

My guess is since the html is getting sent to a new tab, the root URL is not set. 我的猜测是,由于html被发送到新选项卡,因此未设置根URL。 So when it comes to a relative path for the images, css and script files, it doesn't know how to build the full path. 因此,当涉及图像,css和脚本文件的相对路径时,它不知道如何构建完整路径。

Does anyone know of a way to get this method to work? 有谁知道使这种方法起作用的方法? I know I can get this to work by having the server execute the search again, but I'm trying to see if there's a client side solution. 我知道我可以通过让服务器再次执行搜索来使其工作,但是我正在尝试查看是否有客户端解决方案。

Thanks! 谢谢!

Using JavaScript to send the output to another tab is probably the source of your bug, and totally unnecessary. 使用JavaScript将输出发送到另一个选项卡可能是您的错误的根源,并且完全没有必要。 In your css for the page: 在页面的CSS中:

@media screen {
    .menu {display : block;}
}
@media print {
    .menu {display : none;}
}

will do the job. 会做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM