如何在浏览器之间选择`window.URL.createObjectURL()`和`window.webkitURL.createObjectURL()` [英] How to choose between `window.URL.createObjectURL()` and `window.webkitURL.createObjectURL()` based on browser
本文介绍了如何在浏览器之间选择`window.URL.createObjectURL()`和`window.webkitURL.createObjectURL()`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从Firefox开发者网站,我知道Firefox使用
From the Firefox developer website, I know that Firefox uses
objectURL = window.URL.createObjectURL(file);
获取文件类型的URL,但在chrome和其他webkit浏览器中,我们有 window.webkitURL.createObjectURL()
用于检测url。
to get url of file type, but in chrome and other webkit browsers we have window.webkitURL.createObjectURL()
for detecting url.
我不知道如何根据浏览器引擎交换这个功能,我需要它在两个浏览器(Chrome和Firefox)上工作。
I don't know how to swap this functions based on browser engines, and I need it to be worked on both browsers (Chrome and firefox)
https://developer.mozilla。 org / en / DOM / window.URL.createObjectURL
推荐答案
你可以定义一个包装函数:
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;
}
}
然后:
// works cross-browser
var url = createObjectURL( file );
这篇关于如何在浏览器之间选择`window.URL.createObjectURL()`和`window.webkitURL.createObjectURL()`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文