检测浏览器对html文件输入元素的支持 [英] Detect browser support of html file input element

查看:94
本文介绍了检测浏览器对html文件输入元素的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法用JavaScript / jquery / crazy-css-hack来检测用户的浏览器是否支持呈现功能< input type =file/> 文件上传元素?例如,iOS上的safari浏览器不会呈现该元素,而是希望向用户显示一条消息,表明该功能不受支持。我知道我可以检查用户代理并检查它是否是iphone / ipad / etc,但我不知道其他浏览器做了什么或不支持它,或将来还是不会支持它。

解决方案

Galambalazs的回答指出,我只能在iOS的正确方向。我最终使用了这个:

 函数supportsFileInput(){
var dummy = document.createElement(input) ;
dummy.setAttribute(type,file);
return dummy.disabled === false;
}

然而,它不适用于大多数Android设备,因为此功能始终返回true,但呈现带有文字禁用上传的按钮。


Is there a way with a javascript/jquery/crazy-css-hack to detect if the user's browser supports rendering a functional <input type="file" /> file upload element? For example, the safari browser on iOS won't render the element and I would like to instead show a message to the user that the functionality isn't supported. I know I can inspect the user agent and check to see if it an iphone/ipad/etc but I don't know what other browsers do or do not support it or will or will not support it in the future.

解决方案

Galambalazs' answer pointed me in the right direction for iOS only. I ended up using this:

function supportsFileInput() {
  var dummy = document.createElement("input");
  dummy.setAttribute("type", "file");
  return dummy.disabled === false;
}

However, it doesn't work for most Android devices, as this function always returns true but renders the button with the text "Uploads disabled".

这篇关于检测浏览器对html文件输入元素的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆