Dynamically load js files in "use strict"; mode

504 views Asked by At

Working on a Single Page Architecture project, there are a lot of *.js files lazy loaded to DOM.

The file getting included may not have the line "use strict"; but the solution should force "use strict"; for all js files included after loading the initial scripts (Application library files).

1

There are 1 answers

0
Nanang Mahdaen El-Agung On

Maybe you can use ajax to get the scripts text and append the "use strict"; to the scripts and eval() it or append it to body as <script> tag. Example using jQuery:

$.get('foo.js', function(script) {
  $('<script type="text/javascript">').html('"use strict";\r\n' + script).appendTo('body');
  // OR
  eval('"use strict";\r\n' + script);
});