-1

How can I execute my JavaScript after the HTML and other JavaScript has been parsed, but not wait on any other resources (images, stylesheets, etc.)? My JavaScript, which is a user script / Chrome extension, automates clicks on buttons which require other JavaScript to manipulate variables, but don't require other resources.

I am aware of the events load and DomContentLoaded. In my testing, DomContentLoaded does not wait for all of the JS to be parsed. load waits for everything, which is what I'm using now, but not what I want.

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • Create a function with your JS, and call it at the end of the other JS. – Victoria Ruiz Jun 28 '18 at 23:04
  • @VictoriaRuiz I don't have control over the other JS. – jsejcksn Jun 28 '18 at 23:09
  • put your script in a script tag after all the other script tags – Jaromanda X Jun 28 '18 at 23:51
  • @JaromandaX I don't have control over the html either. This is a user script / Chrome extension. I will clarify that in the question. – jsejcksn Jun 28 '18 at 23:53
  • so, is it a user script or a chrome extension? the two are different - user script (tampermonkey) - https://tampermonkey.net/documentation.php#_run_at are your **only** options - as for extension - I'm sure there's documentation available – Jaromanda X Jun 29 '18 at 00:06
  • @JaromandaX In this case they are functionally equivalent: it is a Chrome extension which consists only of a single content script. The same would be produced by dragging a user script into the Chrome extensions window. – jsejcksn Jun 29 '18 at 00:19
  • *In my testing, DomContentLoaded does not wait for all of the JS to be parsed.* It should, even deferred scripts. Are you sure you are not relying on code that do themselves wait for something like the load event? – Kaiido Jun 29 '18 at 06:02
  • @Kaiido When you say "it should" can you point me to the spec or MDN documentation? – jsejcksn Jun 29 '18 at 20:43
  • @jsejcksn https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded along with the note about synchronous scripts, and for [defer]( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attributes) where it says it " will prevent the DOMContentLoaded event from firing until the script has loaded and finished evaluating." – Kaiido Jun 30 '18 at 01:39
  • @Kaiido Thanks for pointing me to the relevant info! I'm really confused why it wasn't working in my testing. Maybe you're right about a dependency waiting for the load event. I'm going to have to parse a lot of lines of code to figure it out. If you could formulate your comment into an answer, I'd like to accept it. – jsejcksn Jun 30 '18 at 01:51
  • I don't really have time right now to compose an answer, and I'm not too sure about what it should say, because the origin of your question os still not clear (you said yourself you knew about DCL). So if I had to write it it would just say use DCL, and I'm sure there are already many fupes of that (though I don't really have time rn to dig them up either) – Kaiido Jun 30 '18 at 02:32

1 Answers1

0

A tricky way to do it: use load and insert a <script> at the end of the document, and put your codes into it.

I believe this WILL work. The point is, browsers parse and run the <script> one by one, in a strict order. So when the browsers get to your <script>, it means the other <script>s have been parsed and executed, but all other resources might not return. I think this is what you want?

terry.qiao
  • 1,959
  • 2
  • 8
  • 10