Detect if browser supports data uri scheme with iframes

后端 未结 2 2076
借酒劲吻你
借酒劲吻你 2020-12-19 13:17

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 browse

2条回答
  •  不思量自难忘°
    2020-12-19 13:51

    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.

提交回复
热议问题