Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Last active December 22, 2015 13:29
Show Gist options
  • Save fastcodecoq/6479812 to your computer and use it in GitHub Desktop.
Save fastcodecoq/6479812 to your computer and use it in GitHub Desktop.
void require( src_file *, where) Función para insertar scripts js de formá rápida en nuestros proyectos
//Licencia GPL v3
function require( src_file , where){
if(! where )
var where = "";
switch(where){
case "head":
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = src_file;
head.appendChild(script);
break;
case "body":
var body = document.getElementsByTagName("body")[0];
var script = document.createElement("script");
script.src = src_file;
body.appendChild(script);
break;
default:
document.write("<script src=\"" + src_file + "\"></script>");
}
}
function require_once( src_file , where){
if(! where )
var where = "";
var scripts = document.getElementsByTagName("script");
var i = 0;
for(;i < scripts.length; i++)
if(scripts[i].getAttribute("src") === src_file)
return false;
switch(where){
case "head":
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = src_file;
head.appendChild(script);
break;
case "body":
var body = document.getElementsByTagName("body")[0];
var script = document.createElement("script");
script.src = src_file;
body.appendChild(script);
break;
default:
document.write("<script src=\"" + src_file + "\"></script>");
}
}
//Ejemplos de Uso
// require("ruta/a/mi/archivo.js"); //inserta el script justo en el lugar del documento donde es llamada la función
// require("ruta/a/mi/archivo.js", "head"); // inserta el script en el head
// require("ruta/a/mi/archivo.js", "body"); // inserta el script al final del body
// *happy coding* by @gomosoft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment