0

I need to know when a script is loaded and it's code is executed so that I can make sure the variables are not undefined. I searched and did not find any question like this one... again I need loaded-executed event not just download-complete.

and I will use 'id' on each script so I can find which is loaded.

<script src="utils.js" id="js_utils"></script>
  • Can't you just add one line of code at the end of the script? Or use an interval to see if a value set by the script is set, starting to run as soon as the document is ready? – Eran Boudjnah Jan 04 '14 at 00:00
  • yea, but it sounds like brute force –  Jan 04 '14 at 00:01
  • It is brute force. The first option isn't, though. – Eran Boudjnah Jan 04 '14 at 00:04
  • I'm messing around with CreateEvent, but I'm not sure all browsers support it. –  Jan 04 '14 at 00:05
  • Here's another approach: learn the order of execution, and use it to your advantage. Unless, of course, you're loading scripts dynamically: http://stackoverflow.com/questions/8996852/load-and-execute-order-of-scripts – Eran Boudjnah Jan 04 '14 at 00:06

1 Answers1

0

If you're not loading scripts dynamically, you can rely on their order of running (which would be the order in which they appear in the document), see here:

load and execute order of scripts

Otherwise, you can either:

Add a line of code to the end of each script file loaded dynamically that runs your block of code.

or

If you don't have the ability to edit those files, start an interval as soon as you inject them into the code, tracking the object you want set before running your block of code.

Community
  • 1
  • 1
Eran Boudjnah
  • 1,228
  • 20
  • 22