Is there a way to load a script tag and execute the script it contains after the page is fully loaded?

  javascript, php, three.js, xmlhttprequest

I’m trying to include a three.js module type script tag dynamically after the page has loaded in my html (or php) page. This I need because google pagespeed insight fails badly on a page where there is three js, giving errors and terrible speed score for the page.

  • I have tried php readfile – works, but not really changing the behaviour of the speed test, I get questionmark result for LCP

  • I was thinking about trying to insert the script with xhr after page load, would that be a solution maybe?

settimeout is not an option here

Any ideas?

Source: Ask PHP