javascript 跨浏览器打印命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7458948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cross-browser print command?
提问by Basit
i want to know if there is any cross-browser print code, that is if i need other then just simple:
我想知道是否有任何跨浏览器打印代码,也就是说,如果我需要其他,那么简单:
//print page
$('.print').click(function() {
window.print();
return false;
});
i did found for bookmark and thats why i was more concern about print too, but couldn't find anything useful on google.
我确实找到了书签,这就是为什么我也更关心打印,但在谷歌上找不到任何有用的东西。
following code is for bookmark cross-browser
以下代码用于书签跨浏览器
//bookmark page
$("a.bookmark").click(function(e)
{
e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
var bookmarkUrl = this.href;
var bookmarkTitle = this.title;
if (window.sidebar) { // For Mozilla Firefox Bookmark
window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
} else if( window.external || document.all) { // For IE Favorite
window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
} else if(window.opera) { // For Opera Browsers
$("a.jQueryBookmark").attr("href",bookmarkUrl);
$("a.jQueryBookmark").attr("title",bookmarkTitle);
$("a.jQueryBookmark").attr("rel","sidebar");
} else { // for other browsers which does not support
alert('Your browser does not support this bookmark action');
return false;
}
});
回答by broofa
window.print() is a de-facto standard. (it's been supported since the days of IE4/Netscape 4).
window.print() 是事实上的标准。(从 IE4/Netscape 4 开始就支持它)。
While you're at it, be sure to check out how you can customize how your page looks when it's printed using print-specific CSS stylesheets.
当您使用它时,请务必查看如何自定义您的页面在使用打印特定的 CSS 样式表打印时的外观。
回答by Marko
window.print()
will do the job.
window.print()
会做的工作。
回答by Daniel A. White
That is the general way. It is not an official part of the dom. I would check for its existence first.
这是一般的方法。它不是 dom 的官方部分。我会先检查它的存在。