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.
Asked
Active
Viewed 73 times
0
-
Which library are you using? It likely has defined its own events that you'll have to use. – Jonathan Lonowski May 12 '16 at 18:07
-
I am using bpmn.io [bpmn io](https://github.com/bpmn-io) . – iroh May 12 '16 at 18:09
2 Answers
1
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.
e.g.
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.

Quentin
- 914,110
- 126
- 1,211
- 1,335
-
somehow this code is working can you tell me why ? window.onload = setTimeout(foo,0); @Quentin – iroh May 12 '16 at 18:14
-
No. You haven't provided enough code to understand all the behaviours. The use of `onload` is entirely pointless there though, you can only assign a function to it and the return value of `setTimeout` is a number. – Quentin May 12 '16 at 18:17
-
If I use window.onload = setTimeout(foo,0); then foo gets executed after the loading of page on all browsers however window.onload=foo does not work, it executes foo before pageload. – iroh May 12 '16 at 18:20
-
@iroh — That doesn't make sense. `window.onload=foo` calls `foo` when the load event fires. `window.onload = setTimeout(foo,0);` calls set timeout immediately, assigns a number to `onload` (uselessly) and then calls `foo` after the minimum amount of time for a timeout has passed (or when the event loop becomes free, whichever occurs later) – Quentin May 12 '16 at 18:22
-
I know it doesn't make sense, but it actually is happening. Anyways thanks, and if you don't believe me then try it out sometime when you're free. It can't be out of sheer luck in both chrome and IE. – iroh May 12 '16 at 18:24
-
Doesn't work — http://jsbin.com/lepiho/1/edit?html,output — It's just a timing coincidence. It just happens to be that the the minimum time out value is longer than the time taken to finish loading the page and start the load event function … on your system … under its current load. – Quentin May 12 '16 at 18:41
0
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.
-
self answers are fine, but with some few requirements. Ensure you give credit to any helpful answers, if its just a small tweak on one of those than accept that and add an *UPDATE* note to question post that shows you used it to answer question. If your answer is too different from other posts, wait 48hr then create it and accept it so marked as such. In both cases it has to clearly state how other answers (at that time) did not suffice. http://stackoverflow.com/help/how-to-ask, http://stackoverflow.com/help/someone-answers, http://stackoverflow.com/help/self-answer – Daniel Brose May 16 '16 at 05:49