等待js脚本加载到页面上

Vla*_*ics 3 javascript dart

我正在尝试将 js 文件添加到页面。如何检查该文件是否已加载?

javascript ga.js 文件:

this.event = function (value) {
    this.value = value;
    this.method = function() {
        return "sometext";
    };
};
Run Code Online (Sandbox Code Playgroud)

飞镖代码:

ScriptElement ga = new ScriptElement()
    ..src = "/ga.js"
    ..async = true;
querySelector('body').append(ga);

bool exist = context.hasProperty('event');
    JsObject event = null;
    if (exist) {
        print("event exist");
    } else {
        print("there is no event yet");
    }
Run Code Online (Sandbox Code Playgroud)

Nie*_*jes 5

您可以将onload事件添加到scriptHTML 中的任何元素。

ga.onload = function(ev) { alert('loaded!') };
Run Code Online (Sandbox Code Playgroud)

由于这是原生HTMLScriptElement行为,因此它应该与 Dart 完美结合。