在另一个js文件中加载外部js文件

时间:2021-01-18 16:03:12

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?

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

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

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

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

1 个解决方案

#1


51  

If you want to include a JS file in a JS you can use jQuery for that

如果你想在JS中包含一个JS文件,可以使用jQuery

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

And if you cannot use jQuery

如果你不能使用jQuery

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

Source

资源

#1


51  

If you want to include a JS file in a JS you can use jQuery for that

如果你想在JS中包含一个JS文件,可以使用jQuery

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

And if you cannot use jQuery

如果你不能使用jQuery

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

Source

资源