简体   繁体   English

如何可靠地确定浏览器是否支持 DataTransfer() 构造函数?

[英]How can I reliably determine if a browser supports the DataTransfer() constructor?

I have a file uploader that uses new DataTransfer() to generate and display thumbnails for uploaded images.我有一个文件上传器,它使用new DataTransfer()来生成和显示上传图像的缩略图。 For Safari users, this constructor is not supported .对于 Safari 用户, 不支持此构造函数。 The tricky part is that DataTransfer as a feature is supported - it's specifically the constructor that is not.最棘手的部分是DataTransfer功能支持-这是专门不是构造函数。

How can I detect if this feature exists, so that I can provide a standard file input for Safari users?如何检测此功能是否存在,以便为 Safari 用户提供标准file输入? To clarify, I'm not looking for help with the fallback - just the feature detection itself.澄清一下,我不是在寻求回退方面的帮助 - 只是特征检测本身。

I attempted the following to no avail:我尝试了以下方法无济于事:

const isConstructor = (func) => (func && typeof func === "function" && func.prototype && func.prototype.constructor) === func;

isConstructor(DataTransfer);  //true in Safari

I was able to work around this, but in a somewhat hacky fashion that I'm not quite happy with.我能够解决这个问题,但以一种我不太满意的方式。 I'll still take any better answers I can get.我仍然会接受我能得到的任何更好的答案。

 let dataTransferConstructorSupported = false; try { new DataTransfer(), dataTransferConstructorSupported = true } catch {} console.log(dataTransferConstructorSupported);

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

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