1

I have two .js files. I want to "read" the second file after the function of the first .js file ends. How do i do that?

html file:

<div class="row connectedSortable" id="sortable1" ng-app="graficosApp" ng-controller="graficosAppCtrl">
    <button ng-click="loadElements()">Load Elements</button>
</div>

first javascript file (angularjs):

var app = angular.module('graficosApp', []);
    app.controller('graficosAppCtrl', function ($scope, $http) {

        $scope.CarregarGraficos = function () {

            var myEl = angular.element(document.querySelector('#sortable1'));        
            myEl.prepend(content);

        }
    });

and then i have my second javascript file, that will add some functionalities to buttons added on the angularjs file. i want to load the second file after the first file.

Thank you.

5
  • Why do you want to "read" a JavaScript file? You should really provide more specific details on what your problem is here. Also try including code samples. Also, how is this specifically related to AngularJS? Commented Jun 28, 2017 at 20:06
  • the first file will add some divs in the html via angularjs. the second file will make some buttons in those added divs work Commented Jun 28, 2017 at 20:10
  • Have you tried anything? Like, say, putting the first file before the second file in the HTML file? Commented Jun 28, 2017 at 20:18
  • yes, i'd try that. but the browser loads the second file when i load the html page, and the content is added after that, so it doesn't work Commented Jun 28, 2017 at 20:19
  • After read some ideas about it i'd quit and added the second file content inside the loadElements function. Thank you all for the effort Commented Jun 28, 2017 at 20:30

3 Answers 3

1

I am not sure that adding additional functionality in such approach is good, but anyway here is answer to your question. To load some scripts when you need - you can run this code when everything is done in first file.

// ... everything necessary is done before
var scriptEl = document.createElement('script');
scriptEl.setAttribute('src', '/path_to_js_file.js');
document.body.appendChild(scriptEl);

Anyway, I suggest you to think once more about your code structure and functionality and try to avoid situations when you need to use such 'hacks'.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using jQuery, you can getScripts quite easily:

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

Or use ajax:

$.ajax({
  url: "http://myscript/path.js",
  dataType: "script",
  success: function() {
    console.log("script loaded");
  }
});

Comments

0

You can try loading the FIRST file in HEAD tag; So this will ensure that it will block the execution. And the SECOND file you can 'defer' its execution.

<script src='FIRST_FILE'></script>
<script defer src='source/SECOND_FILE'></script>

I am not sure about the expected behaviour what you want to achieve; As you could simply load "Functions" that are required after primary functions are executed.

2 Comments

It should work only if everything in first file is synchronous. In any other case there is no guarantee that script will be loaded and executed after everything in first script was done.
exactly! Thats a assumption

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.