我想在页面准备好后获取有关某些 DOM 元素的一些信息,但我还没有弄清楚如何判断这是什么时候。我试过使用document.on.contentLoaded
and document.on.readyStateChange
,但似乎都不起作用。在下面的代码中,onContentLoaded()
永远onReadyChanged()
不会被调用。
class WhiteTrace {
WhiteTrace() {
}
void onContentLoaded(Event e) {
print("onContentLoaded");
// This never gets called
}
void onReady() {
print("onReady");
// Do stuff
}
void onReadyChanged(Event e) {
print("onReadyStateChanged");
// This never gets called
if (document.readyState == "complete") {
onReady();
document.on.readyStateChange.remove(onReadyChanged);
}
}
void onResize(Event e) {
// Do stuff
}
void run() {
write("Hello World!");
document.on.contentLoaded.add(onContentLoaded);
window.on.resize.add(onResize);
document.on.readyStateChange.add(onReadyChanged);
print("readyState: " + document.readyState);
if (document.readyState == "complete") {
document.on.readyStateChange.remove(onReadyChanged);
onReady();
}
}
void write(String message) {
// the HTML library defines a global "document" variable
document.query('#status').innerHTML = message;
}
}
void main() {
new WhiteTrace().run();
}