A better test for base64 URI support (can I create a large base64-encoded image in JS?)

后端 未结 1 1330
别那么骄傲
别那么骄傲 2021-01-05 11:26

I\'m using Modernizr to detect the features supported in the browser our users are running, so far so good. But I\'ve come up against a theoretical problem when testing for

1条回答
  •  孤城傲影
    2021-01-05 12:02

    I think I have an answer. I tried all sorts of techniques (repeated text chunks in the PNG source that I could manually add, etc) until I found that adding line breaks appears to do the job:

        var b64test = new Image();
        b64test.onload = function() {
           alert("yay!")
        }
    
        b64test.onerror = function() {
           alert("boo")
        }
    
        /* A 1x1 GIF image */
    
        var base64str = "R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="
        while (base64str.length < 33000) {
            base64str = "\r\n" + base64str;
        }
    
        b64test.src= "data:image/gif;base64," + base64str;
    

    Fails in IE8, works in IE9 and others. I'd love to hear any alternatives, though.

    0 讨论(0)
提交回复
热议问题