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

printing - Feature detection solution for window.print() JavaScript - Stack Overflow

programmeradmin5浏览0评论

Question

Is there any way to detect support for 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.

Background

The following links lead me to believe that:

  • Apple requires all 3rd party browsers to use UIWebView
  • UIWebView as used in non-Safari apps does nothing when you run window.print()
  • Safari on iOS does something appropriate when you run window.print()

The links:

  • !topic/chrome/7wTj1dteSeI
  • Javascript printing on mobile devices(ios)

P.S. I looked in the Modernizr docs to see if it can detect support for printing, but didn't spot anything.

P.P.S. I see that Chrome for iOS does support printing, albeit through GCP. Here, just looking for feature detection rather than a print solution. (;ref_topic=1719889)

Edit To clarify, it appears that window.print is not part of any standard: .print. If window.print exists in some browsers but doesn't do anything useful (e.g., isn't wired up to any browser-specific implementation of appropriate printing functionality) that is ultimately what I want to detect.

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; but the call may be a No op, or may send a request the the operating system that the OS simply ignores.

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.

Question

Is there any way to detect support for 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.

Background

The following links lead me to believe that:

  • Apple requires all 3rd party browsers to use UIWebView
  • UIWebView as used in non-Safari apps does nothing when you run window.print()
  • Safari on iOS does something appropriate when you run window.print()

The links:

  • https://productforums.google./forum/#!topic/chrome/7wTj1dteSeI
  • Javascript printing on mobile devices(ios)
  • https://stackoverflow./a/4362472/749227
  • http://daringfireball/linked/2012/06/28/chrome-ios
  • https://stackoverflow./a/4362472/749227

P.S. I looked in the Modernizr docs to see if it can detect support for printing, but didn't spot anything.

P.P.S. I see that Chrome for iOS does support printing, albeit through GCP. Here, just looking for feature detection rather than a print solution. (https://support.google./chrome/answer/3049815?hl=en&ref_topic=1719889)

Edit To clarify, it appears that window.print is not part of any standard: https://developer.mozilla/en-US/docs/Web/API/Window.print. If window.print exists in some browsers but doesn't do anything useful (e.g., isn't wired up to any browser-specific implementation of appropriate printing functionality) that is ultimately what I want to detect.

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; but the call may be a No op, or may send a request the the operating system that the OS simply ignores.

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.

Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Feb 4, 2014 at 23:56 jinglesthulajinglesthula 4,5875 gold badges47 silver badges84 bronze badges 5
  • Duplicate of: stackoverflow./questions/9268840/… (can't flag because of bounty) – Martin Tournoij Commented Feb 13, 2014 at 18:39
  • The answer that (ab)uses the blocking nature of print() is your best guess. I had the same idea, can't think of anything better. It doesn't actually detect support though, it just tells you if window.print() actually did something... – Martin Tournoij Commented Feb 13, 2014 at 18:40
  • 1 @Carpetsmoker would you mind adding a reference to the duplicate question (and the answer you referred to) as an answer on this question? I'll toss you the bounty and accept your answer if you do, as that's about as 'correct' an answer to a duplicate as I can think of :) – jinglesthula Commented Feb 13, 2014 at 20:57
  • Cool, I actually found this on another forum. Should I just delete my answer? seeing as the abuse of negative votes didn't help me at all. I was just trying to help you find a solution, but seeing as you already accepted your own answer, I probably should think twice before I try helping anyone here. – Bobby5193 Commented Feb 13, 2014 at 23:06
  • No offence intended, mate. Honestly none of the downvotes for your answer were from me. If someone posts a question and later discovers the answer (even if the answer is "oh, there's a duplicate I didn't notice before") it's appropriate to post that as the answer and, if no one else offers an answer that's more acceptable, to accept one's own answer. Even downvoted answers can provide useful information about the question, so I think it increases the usefulness of SO to not remove it. – jinglesthula Commented Feb 14, 2014 at 22:04
Add a ment  | 

2 Answers 2

Reset to default 2

This question is indeed a duplicate of Determine whether browser supports printing. See carpetsmoker's ment on this question and the answer referred to. 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. 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');
}
发布评论

评论列表(0)

  1. 暂无评论