Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

javascript - How to know if window "load" event was fired already

I'm writing a Javascript script. This script will probably be loaded asynchronously (AMD format).

In this script, I'd like to do nothing important until the window.load event was fired. So I listen to the window "load" event.

But if the script is loaded after window.load event... how can I know window.load was already fired?

And of course I don't want to add something in any other scripts (they are all loaded async, the problem is the same) :)

Edit :

Imagine an HTML doc with no Javascript in it at all.

Than someone insert in this doc a tag, and this script tag loads my Javascript file.

This will execute my script.

How this script can know if window.load was already fired ?

No jQuery, not any script in the HTML doc before mine.

Is it possible to know ??

I found the window.document.readystate property. This property is for document "ready" event I guess, not for window "load". Is there anything similar for window "load" event ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easiest solution might be checking for document.readyState == 'complete', see http://www.w3schools.com/jsref/prop_doc_readystate.asp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...