3 ms·
Yes, the placement of the script tag very much matters, but it's most likely after your html or deferred to run after your html, in which case... Not really. T
by 8n4vidtmkvmk 3y ago
Yes, the placement of the script tag very much matters, but it's most likely after your html or deferred to run after your html, in which case... Not really.
There is one little quirky js thing you can do to get the script element from inside the script but I doubt many people are doing that.
- chrismorgan 3y agoI do stuff like this regularly: <div>…</div><script>document.currentScript.previousSibling.…</script> Provided you care even vaguely about code size (and are ending up with handfuls of kilobytes at most, not multiple megabytes), inlining everything is excellent for performance (seriously, unreasonably effective, way more than most expect), and if it’s stuff that must be done before the document is fully interactive, then doing it immediately like this rather than messing around with DOMContentLoaded event handlers and querying and such is likewise excellent for performance.
- 8n4vidtmkvmk 3y agoYeah I think I was doing something similar. The scripts can safely be executed as soon as the HTML/DOM is in there, no need to defer to the end.
- Eduard 3y ago> There is one little quirky js thing you can do to get the script element from inside the script. Now you *have* to elaborate. And what's the problem it solves?
- 8n4vidtmkvmk 3y agoI think it's just document.currentScript I used it years ago because we had an XML based templating engine and some "components" needed to inject little scripts to add functionality. I could have given everything unique IDs but this was more fun. Forget what I did with it but I need to know where it was in the DOM or something.