Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

javascript - How to remove print header/footer with code

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 comes 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>"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...