在另一个js文件中加载外部js文件 [英] Load external js file in another js file

查看:88
本文介绍了在另一个js文件中加载外部js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个文件包含在我的HTML中,我想从另一个javascript中调用它。
请建议我该怎么做?

I have this file include in my html and I want to call it from another javascript. Please suggest me how should I do it?

<script type="text/javascript" src="js.js">/*
  old:123
  new: 456*/
</script>

我想将它包含在我的js文件中,而不是html中。

I want to include it in my js file, not in the html.

推荐答案

如果要在JS中包含JS文件,可以使用 jQuery.getScript()

If you want to include a JS file in a JS you can use jQuery.getScript() for that

$.getScript('another_file.js', function() {
    //script is loaded and executed put your dependent JS here
});

如果你不能使用jQuery

And if you cannot use jQuery

var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);

来源

有关W3当前CSP3规范的更多信息,请查看这里

For more information about the current CSP3 spec from W3 look here.

这篇关于在另一个js文件中加载外部js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆