在香草JavaScript中使用JSONP通过创建<script>
标记和src
,让我考虑在JavaScript插件和其他利用库的JavaScript组件中进行依赖注入,但希望作为独立尽可能。通过动态的JavaScript依赖注入<script>标记
使用jQuery作为一个受欢迎的库,有可能需要注入的例子...
// make sure we don't load it if already loaded.
if (typeof jQuery === 'undefined') {
var dependency = document.createElement('script');
dependency.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(dependency);
}
window.onload = function() {
// jQuery is now available w/o the extra <script> tag added by the end user...
}
真的在这一点上它的主要概念(但工作),但我想知道如果这将被认为是JavaScript依赖注入的可接受解决方案?对此有何想法?
好主意,谢谢! – jondavidjohn