26

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
  • 14,686
  • 3
  • 31
  • 50
Gaurav
  • 409
  • 1
  • 5
  • 12

1 Answers1

66

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
  • 684
  • 1
  • 7
  • 23
doitlikejustin
  • 6,213
  • 2
  • 39
  • 67