Skip to content

Instantly share code, notes, and snippets.

@ElfhirDev
Created June 5, 2013 14:46
Show Gist options
  • Save ElfhirDev/5714384 to your computer and use it in GitHub Desktop.
Save ElfhirDev/5714384 to your computer and use it in GitHub Desktop.
Include a js script in a js script
function include(src, attributes)
{
try {
attributes = attributes || {};
attributes.type = "text/javascript";
attributes.src = src;
var script = document.createElement("script");
for(aName in attributes)
script[aName] = attributes[aName];
document.getElementsByTagName("head")[0].appendChild(script);
return true;
} catch(e) { return false; }
}
// Load the file ; be careful of the path
function test() {
include('monfichier.js');
}
// then you can call object and var in 'monfichier.js'
/////////////////////////////////////////////
// Exemple 1
//
// var file = "monfichier.js";
// include(file);
//
// Exemple 2
// var file = "http://www.example.com/script.js";
// var attributes = {charset:"utf-8"};
//
// include(file, attributes);
//
// Return true if it can load the file.
//////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment