检测浏览器是否支持带iframe的数据uri方案 [英] Detect if browser supports data uri scheme with iframes

查看:171
本文介绍了检测浏览器是否支持带iframe的数据uri方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Internet Explorer不支持iframe网址的数据uri方案(请参阅 http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx )。其他浏览器。由于浏览器检测加载了测试和面向未来的问题,我想使用功能检测来解决这个问题。

Internet Explorer does not support the data uri scheme for iframe urls (see http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx). Other browsers do. As browser detection is loaded with testing and future-proofing problems, I want to use feature detection to work around this issue.

那么:如何检测浏览器是否支持iframe的数据uri方案?

So: how can I detect whether or not a browser supports the data uri scheme for iframes?

推荐答案

由Kevin Martin提供的此解决方案经过测试,似乎在IE,FF和Chrome中给出正确的结果:

This solution by Kevin Martin is tested and seems to be giving the correct result in IE, FF and Chrome:

function iframeDataURITest(src) {
    var support,
        iframe = document.createElement('iframe');

    iframe.style.display = 'none';
    iframe.setAttribute('src', src);

    document.body.appendChild(iframe);

    try {
        support = !!iframe.contentDocument;
    } catch (e) {
        support = false;
    }

    document.body.removeChild(iframe);

    return support;
}

console.log('Empty data uri', iframeDataURITest('data:;base64,'));
console.log('"*" data uri', iframeDataURITest('data:text/html;base64,Kg=='));

与其他一些建议不同,它是同步的 - 不需要混乱超时或回调。

Unlike some of the other suggestions, it is synchronous - no need to mess around with timeouts or callbacks.

这篇关于检测浏览器是否支持带iframe的数据uri方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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