0

How i can include a javascript file in other javascript?

Its frontend javascript.

I not want to include in html, i want like in nodeJS:

var jquery = require("jquery.js");
jquery.$("#c").style.color = "green";

I tried this:

const jqLink = 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js';
/* I try this, what give me a error, with requireJS */
define([jqLink], function(jq){
 return jq;
});

jq$("#c").style.color = "red";
<html><head><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script>
</head>
<body><h1 id="h1tag">Im h1tag</h1>
</body>

PD: i tried this too:

Main.js :

 window.addEventListener("DOMContentLoaded",main);

function main(){
    scripts();
    var e = new ES();
    e.get("#ti").style.opacity = +true;
}

function scripts(){
    var list = ["es.js"], script = document.createElement("script"),b = document.head, i=0,max = list.length;
    for(;i<max;i++){
    script.src = list[i];
    b.appendChild(script);  
    }
}

ES.JS:

class ES{
    constructor(){  
    }
    get(symbol, e){
    let d = document;
    return symbol === "#" ? d.getElementById(e) : symbol === "." ? d.getElementsByClassName(e) : d.getElementsByTagName(e);
    }
}
ESCM
  • 269
  • 2
  • 13

1 Answers1

0

This is just a tweak that might work. It loads your "included" script into the same page

function includeFile(path) {
    var imported = document.createElement('script');
    imported.src = path;
    document.head.appendChild(imported);
}

Call it like:

includeFile('/path/to/other_js_file.js');
mrid
  • 5,782
  • 5
  • 28
  • 71