简体   繁体   English

window.print()JavaScript的功能检测解决方案

[英]Feature detection solution for window.print() JavaScript

Question

Is there any way to detect support for window.print()? 有什么方法可以检测对window.print()的支持吗? I'd prefer to detect the feature itself vs trying to go down the rabbit hole of detecting whether I'm on a mobile browser or desktop or iOS or Android or which particular mobile browser I might be on. 我宁愿检测功能本身,也不愿意尝试检测我是使用移动浏览器还是台式机,iOS或Android或我可能使用的是哪种特定的移动浏览器。

Background 背景

The following links lead me to believe that: 以下链接使我相信:

  • Apple requires all 3rd party browsers to use UIWebView Apple要求所有第三方浏览器都使用UIWebView
  • UIWebView as used in non-Safari apps does nothing when you run window.print() 在非Safari应用中使用的UIWebView在运行window.print()时不执行任何操作
  • Safari on iOS does something appropriate when you run window.print() 当您运行window.print()时,iOS上的Safari会执行适当的操作

The links: 链接:

PS I looked in the Modernizr docs to see if it can detect support for printing, but didn't spot anything. PS:我查看了Modernizr文档,看它是否可以检测到对打印的支持,但没有发现任何问题。

PPS I see that Chrome for iOS does support printing, albeit through GCP. PPS我发现,尽管适用于GCP,但iOS版Chrome确实支持打印。 Here, just looking for feature detection rather than a print solution. 在这里,只寻找特征检测而不是打印解决方案。 ( https://support.google.com/chrome/answer/3049815?hl=en&ref_topic=1719889 ) https://support.google.com/chrome/answer/3049815?hl=zh_CN&ref_topic=1719889

Edit To clarify, it appears that window.print is not part of any standard: https://developer.mozilla.org/en-US/docs/Web/API/Window.print . 编辑为了澄清起见,window.print似乎不是任何标准的一部分: https : //developer.mozilla.org/en-US/docs/Web/API/Window.print If window.print exists in some browsers but doesn't do anything useful (eg, isn't wired up to any browser-specific implementation of appropriate printing functionality) that is ultimately what I want to detect. 如果window.print在某些浏览器中存在但没有做任何有用的事情(例如,没有连接到适当的打印功能的任何特定于浏览器的实现),那最终就是我要检测的东西。

My guess is that mobile browser vendors will have something defined for window.print so that any scripts that try to call it won't error; 我的猜测是,移动浏览器供应商将为window.print定义一些内容,以便任何尝试调用它的脚本都不会出错; but the call may be a No op, or may send a request the the operating system that the OS simply ignores. 但是调用可能是“否”操作,或者可能向OS完全忽略的操作系统发送请求。

The more I think about how the guts of this could be working the less hopeful I am of a simple JavaScript approach to detecting the feature, which is too bad. 我越想这方面的工作原理,我对使用简单的JavaScript方法检测该功能的希望就越小,这太糟糕了。

This question is indeed a duplicate of Determine whether browser supports printing . 这个问题确实与确定浏览器是否支持打印重复。 See carpetsmoker's comment on this question and the answer referred to. 请参阅petersmoker对这个问题的评论以及所提及的答案。 I agree, that may be the best we can do for now. 我同意,这可能是我们目前可以做的最好的事情。

Even if window.print isn't part of any standard, it'd be nice if browser vendors implemented and agreed upon some API for detection. 即使window.print不是任何标准的一部分,但如果浏览器供应商实现并同意一些用于检测的API,那将是很好的。 Perhaps they're all internally working on an appropriate implementation for the function and so don't want to throw out there something temporary that'll be useless later. 也许他们在内部都在为该函数开发适当的实现,因此不想在此扔掉一些暂时没有用的临时内容。 Oh, well. 那好吧。

Updated 更新

Try this: 尝试这个:

function supportPrint() {   
     return (typeof(window.print) === 'function');
}

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

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