How to run a script which was recently added to <head> tag?

I have added a script containing the link to a JavaScript file hosted on GitHub, to the <head> tag of a document:

var imported = document.createElement('script');
imported.src = 'GITHUB_FILE.js';
document.head.appendChild(imported);

How could I actually run/execute that recently added script so that the GitHub JS file would be “included” in that script (similar to a PHP include)?

Why are we using Javascript to add a static file?

Hi @bendqh1, a script added this way should actually run… if you need access to global variables added by that script, you might add a load listener like so:

const script = document.createElement('script')

script.addEventListener('load', () => {
  window.somethingGlobal()
})

script.src = 'script.js'
document.head.appendChild(script)

That static file should be called into about three different user script managers (each one in another browser type: Tampermonkey for Chrome, Greasemonkey for Firefox and Violentmonkey for Edge);
I desire to call it with JavaScript just to maintain only one version (i.e. the one in GitHub) instead three different versions (i.e. one in each user script manager).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.