0

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 May 12, 2016 at 18:05
2
  • Which library are you using? It likely has defined its own events that you'll have to use. Commented May 12, 2016 at 18:07
  • I am using bpmn.io bpmn io . Commented May 12, 2016 at 18:09

2 Answers 2

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.

answered May 12, 2016 at 18:11
Sign up to request clarification or add additional context in comments.

6 Comments

somehow this code is working can you tell me why ? window.onload = setTimeout(foo,0); @Quentin
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.
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 — 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)
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.
|
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.

answered May 13, 2016 at 16:20

1 Comment

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. stackoverflow.com/help/how-to-ask, stackoverflow.com/help/someone-answers, stackoverflow.com/help/self-answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.