最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove print headerfooter with code - Stack Overflow

programmeradmin2浏览0评论

I am using the following code to print a page within my application...

<html><body onload=""window.print();"">"
  sHtmlBody = sHtmlBody & "<body>"

The window.print() is working fine. I know once the print es up I can manually go into the settings and remove headers and footer. On IE I know that I have to go to print preview and then remove the print headers.

However, is there some line of code which does this automatically so the users of the application don't have to do this?

EDIT:

 sHtmlBody = "<style type='text/css'>"
      sHtmlBody = sHtmlBody & " @media print{"
      sHtmlBody = sHtmlBody & " body{ background-color:#FFFFFF; background-image:none; color:#000000 }"
      sHtmlBody = sHtmlBody & " #ad{ display:none;}"
      sHtmlBody = sHtmlBody & " #leftbar{ display:none;}"
      sHtmlBody = sHtmlBody & " #contentarea{ width:100%;}"
      sHtmlBody = sHtmlBody & " }"
      sHtmlBody = sHtmlBody & " </style>"
      sHtmlBody = sHtmlBody & "<html><body onload=""window.print();"">"
      sHtmlBody = sHtmlBody & "<body>"

I am using the following code to print a page within my application...

<html><body onload=""window.print();"">"
  sHtmlBody = sHtmlBody & "<body>"

The window.print() is working fine. I know once the print es up I can manually go into the settings and remove headers and footer. On IE I know that I have to go to print preview and then remove the print headers.

However, is there some line of code which does this automatically so the users of the application don't have to do this?

EDIT:

 sHtmlBody = "<style type='text/css'>"
      sHtmlBody = sHtmlBody & " @media print{"
      sHtmlBody = sHtmlBody & " body{ background-color:#FFFFFF; background-image:none; color:#000000 }"
      sHtmlBody = sHtmlBody & " #ad{ display:none;}"
      sHtmlBody = sHtmlBody & " #leftbar{ display:none;}"
      sHtmlBody = sHtmlBody & " #contentarea{ width:100%;}"
      sHtmlBody = sHtmlBody & " }"
      sHtmlBody = sHtmlBody & " </style>"
      sHtmlBody = sHtmlBody & "<html><body onload=""window.print();"">"
      sHtmlBody = sHtmlBody & "<body>"
Share Improve this question edited Oct 6, 2021 at 22:44 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 10, 2011 at 8:39 BeginnerBeginner 29.6k65 gold badges160 silver badges239 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

you can do with the help of CSS , before print set the CSS of the page . for example:

<style type="text/css">
@media print{
  body{ background-color:#FFFFFF; background-image:none; color:#000000 }
  #ad{ display:none;}
  #leftbar{ display:none;}
  #contentarea{ width:100%;}
}
</style>

This code when added to the page hides the 2 divs with ids "ad" and "leftbar", plus makes additional changes to the rest of the document when it's printed.

IF you are asking about browser specific settings like print date and time , then i think it is not possible through code. check this out : Remove the default browser header and footer when printing HTML

'@media print': {
  '@page': {
    size: "auto",
    margin: "0mm"
  },
  'body': {
    margin: 0
  }
}
发布评论

评论列表(0)

  1. 暂无评论