js.id=id; -- apply id to newly created script tag ::
js.src=jsfilename; - apply file name in src so it load JavaScript when it embed to HTML DOM ::
fjs.parentNode.insertBefore(js,fjs); - insert script tag :: before all script tag.
as script tag embed to HTML DOM , it load JavaScript from path of src of script tag.
",
"dateCreated": "0000-00-00 00:00:00",
"upvoteCount": 2,
"url": "https://www.arrayoverflow.com/question/how-to-include-a-javascript-file-in-another-javascript-file/241#250",
"author": {
"@type": "Person",
"name": "Phpworker"
}
}
]
}
}
how to include a JavaScript file in another JavaScript file?
how one can include one JavaScript file to another JavaScript file ?
as like css have @import to include another css . do we have same import function or something to include JavaScript file to another JavaScript file
import one.js
.. . ..
//other code here
or php have include and require function to include in one PHP file to another PHP file ,
so we have include function in JavaScript to include another file in one.
//js file
include("one.js");
.. . ..
//other code here
Syntax : imports(filename,id)
filename is file name with full path to include
and id is id of script tag which hold script which is included . for future reference .
LET ME EXPLAIN CODE fjs=document.getElementsByTagName('script')[0]; ask for first script tag from HTML DOM
Now
js=document.createElement('script'); -- create an element named "script" ::<script></script>
js.id=id; -- apply id to newly created script tag :: <script id='twitter-wjs'></script>
js.src=jsfilename; - apply file name in src so it load JavaScript when it embed to HTML DOM :: <script id='twitter-wjs' src="//platform.twitter.com/widgets.js"></script>
fjs.parentNode.insertBefore(js,fjs); - insert script tag :: <script id='twitter-wjs' src="//platform.twitter.com/widgets.js"></script> before all script tag.
as script tag embed to HTML DOM , it load JavaScript from path of src of script tag.