在另一个js文件中加载外部js文件[关闭]

2024-01-01

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

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

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


如果你想在 JS 中包含 JS 文件,你可以使用jQuery.getScript() https://api.jquery.com/jQuery.getScript/为了那个原因

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

如果你不能使用 jQuery

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

Source https://stackoverflow.com/a/4634669/1893629

有关 W3 当前 CSP3 规范的更多信息,请查看here https://www.w3.org/TR/CSP3/#csp-header.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在另一个js文件中加载外部js文件[关闭] 的相关文章

随机推荐