26

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

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

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

4

1 回答 1

66

如果你想在 JS 中包含一个 JS 文件,你可以使用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);

资源

有关来自 W3 的当前 CSP3 规范的更多信息,请查看此处

于 2013-08-15T20:30:37.700 回答