简体   繁体   中英

How to execute a javascript code strictly after the complete page has been loaded?

I have a javascript code that needs to be executed after call to a library is complete, the library generates a canvas on the main page.I have already tried window.onload ,defer ,async, $(window).bind("load", function) but nothing seems to work. The aforementioned library makes a series of JS function calls within itself.I am open to using any plugin, any way as long as it allows me to execute my code after the page has loaded.

There is no way to generically hook in to "When some arbitrary script has finished doing whatever it is doing".

You would need to modify the script so that it calls your function as the last thing it does.

If the script exposes its functions as globals, then you might be able to dynamically rewrite them.

eg

var original_make_canvas();
make_canvas = function make_canvas() {
    original_make_canvas();
    do_something_else();
}

… but there are a lot of ifs and buts to that and the above example would need a very simplistic case.

This is how I solved it. Suppose I have a function foo which needs to be executed after loading of certain element on the page. I used setinterval to call the function every 100 ms till it succeeds after which I used clearInterval. Something like Stop setInterval call in JavaScript .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM