-1

Let's say I have a block of javascript code, and I want to take some of it and host it somewhere else.

Is it possible to then load the now-remote part of the code in the places it was before, as executable code, within the original block, so that the whole thing functions as it did before?

If so, how is this referenced within javascript?

  • you could create `script`-tags with the link to your js, but this will only work if you can clearly seperate the two scripts (you cannot cut them in a loop for example) – Luca Kiebel Jul 28 '17 at 09:42

1 Answers1

0

If your code is a function or a module of some sort wich you can call after it is loaded, you can just load it into the page and call the new ,oaded function. you can use:

function depend( url , callback )
{
  var script = document.createElement( 'script' );
  var scripts = document.getElementsByTagName( 'script' )[0];
  script.async = true;
  script.onload = function()
  {
    script.onload = null;
    callback();
  }
  script.src = url;
  ( document.getElementsByTagName( "head" )[ 0 ] ).appendChild( script );
}

and use it like:

depend('path to script', function( ) {
  // call your function herer....
});
VA79
  • 474
  • 3
  • 7