Internet Explorer 不支持 iframe url 的数据 uri 方案(参见 http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx)。其他浏览器可以。由于浏览器检测加载了测试和面向 future 的问题,我想使用功能检测来解决这个问题。
那么:如何检测浏览器是否支持 iframe 的数据 uri 方案?
最佳答案
This solution by Kevin Martin经过测试,似乎在 IE、FF 和 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=='));
与其他一些建议不同,它是同步的 - 无需处理超时或回调。
关于javascript - 检测浏览器是否支持带 iframe 的数据 uri 方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25123313/