28

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.

putvande
  • 15,068
  • 3
  • 34
  • 50
Gaurav
  • 549
  • 1
  • 5
  • 13

1 Answers1

69

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.

Jghorton14
  • 724
  • 1
  • 8
  • 25
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68