How to choose between `window.URL.createObjectURL()` and `window.webkitURL.createObjectURL()` based on browser

后端 未结 3 1452
失恋的感觉
失恋的感觉 2020-12-30 05:13

From the Firefox developer website, I know that Firefox uses

objectURL = window.URL.createObjectURL(file);

to get url of file type, but in

相关标签:
3条回答
  • 2020-12-30 05:56
    if (window.URL !== undefined) {
        window.URL.createObjectURL();
    } else if (window.webkitURL !== undefined) {
        window.webkitURL.createObjectURL();
    } else {
        console.log('Method Unavailable: createObjectURL');
    }
    

    Is round-about what you're looking for. Also, THIS example uses the much simpler...

    window.URL = window.URL || window.webkitURL;
    
    0 讨论(0)
  • 2020-12-30 05:57

    Simple one liner:

    var createObjectURL = (window.URL || window.webkitURL || {}).createObjectURL || function(){};
    
    0 讨论(0)
  • 2020-12-30 06:04

    You could define a wrapper function:

    function createObjectURL ( file ) {
        if ( window.webkitURL ) {
            return window.webkitURL.createObjectURL( file );
        } else if ( window.URL && window.URL.createObjectURL ) {
            return window.URL.createObjectURL( file );
        } else {
            return null;
        }
    }
    

    And then:

    // works cross-browser
    var url = createObjectURL( file );
    
    0 讨论(0)
提交回复
热议问题