![](/img/trans.png)
[英]Javascript how to create a script tag dynamically without the source file?
[英]How to load a Javascript file without Script tag?
我有一個代碼,我有嵌套<script>標簽,內部腳本標簽什么都不做,但使用src屬性加載另一個Javascript文件。 因為,在我的代碼中,內部腳本標記是關閉外部腳本的。
由於我已經在JavaScript模式下,有沒有辦法加載另一個腳本而不使用<script src =“”>調用?
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "path_to_the_file.js";
document.body.appendChild(js);
但這是異步的,所以你必須等到加載腳本才能使用它 -
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "path_to_the_file.js";
js.onload = function() {
//Code using this script here
};
document.body.appendChild(js);
謝謝@Fletch,我的ES2015等價物:
document.body.appendChild(Object.assign(document.createElement('script'), {
type: 'text/javascript',
src: 'http://external.script.url',
onload: () => this.onScriptLoaded()
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.