Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load external js file in another js file [closed]

I have this file include in my html and I want to call it from another javascript. Please suggest me how should I do it?

<script type="text/javascript" src="js.js">/*   old:123   new: 456*/ </script> 

I want to include it in my js file, not in the html.

like image 672
Gaurav Avatar asked Aug 15 '13 20:08

Gaurav


People also ask

How do I include an external JS file in another JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.

Is it possible to dynamically load external JavaScript files in JavaScript?

Dynamic loadingThose files can be loaded asynchronously in JavaScript. To load a JavaScript file dynamically: Create a script element. Set the src , async , and type attributes.

How do I link two JavaScript files?

Answer: Use the export and import Statement Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.


1 Answers

If you want to include a JS file in a JS you can use jQuery.getScript() for that

$.getScript('another_file.js', function() {     //script is loaded and executed put your dependent JS here }); 

And if you cannot use jQuery

var imported = document.createElement('script'); imported.src = '/path/to/imported/script'; document.head.appendChild(imported); 

Source

For more information about the current CSP3 spec from W3 look here.

like image 157
doitlikejustin Avatar answered Sep 21 '22 08:09

doitlikejustin