1

如果你做得太早,你会得到错误Uncaught ReferenceError: filepicker is not defined

我正在使用来自https://developers.filepicker.io/docs/web/的代码来加载它:

For advanced users, if you want to load the javascript in a non-blocking fashion, you can use this instead:

<script type="text/javascript">
(function(a){if(window.filepicker){return}var b=a.createElement("script");b.type="text/javascript";b.async=!0;b.src=("https:"===a.location.protocol?"https:":"http:")+"//api.filepicker.io/v1/filepicker.js";var c=a.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c);var d={};d._queue=[];var e="pick,pickMultiple,pickAndStore,read,write,writeUrl,export,convert,store,storeUrl,remove,stat,setKey,constructWidget,makeDropPane".split(",");var f=function(a,b){return function(){b.push([a,arguments])}};for(var g=0;g<e.length;g++){d[e[g]]=f(e[g],d._queue)}window.filepicker=d})(document); 
</script>

我不知道如何知道它何时加载,以便我可以调用 filepicker.setKey()。一种解决方法是每次我使用文件选择器时都调用它,但这似乎很蹩脚。

4

1 回答 1

3

非阻塞 js 在脚本加载时将所有调用排队,然后在脚本加载后执行这些调用。包含脚本标记后,您应该能够立即开始调用:

<script type="text/javascript">(function(a){if(window.filepicker){return}var b=a.createElement("script");b.type="text/javascript";b.async=!0;b.src=("https:"===a.location.protocol?"https:":"http:")+"//api.filepicker.io/v1/filepicker.js";var c=a.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c);var d={};d._queue=[];var e="pick,pickMultiple,pickAndStore,read,write,writeUrl,export,convert,store,storeUrl,remove,stat,setKey,constructWidget,makeDropPane".split(",");var f=function(a,b){return function(){b.push([a,arguments])}};for(var g=0;g<e.length;g++){d[e[g]]=f(e[g],d._queue)}window.filepicker=d})(document); </script>
<script>
filepicker.setKey('testing');
filepicker.pick()
</script>

http://jsfiddle.net/bbcbK/1/

于 2013-04-03T16:34:48.067 回答