JavaScript Function not working if src is a variable?

The reason it's not working is that you can't add a src to a script element that's already in the DOM — or rather, doing so doesn't do anything. The script element has already been processed.

Instead, create it and then append it:

var script = document.createElement("script");
script.onload = function() {
    myFunc();
};
script.src = SettingsFile.UrlToMyJS;
document.head.appendChild(script);
// If you need to support IE8, use the following instead of the previous line:
//document.getElementsByTagName("head")[0].appendChild(script);

That waits for the script to load, then calls myFunc (which should exist by then).

Also note that as I and Jeremy pointed out in the comments, body doesn't go in head, it goes after. It's also generally best to put script tags at the end of body (if you're not using async or defer attributes on them or type="module"). So in all, something like:

<head>
    <!-- head stuff here -->
</head>
<body>
    <!-- content here -->
    <script src="https://myurl.de/settingsFile.js"></script>
    <script type="text/javascript">
    var script = document.createElement("script");
    script.onload = function() {
        myFunc();
    };
    script.src = SettingsFile.UrlToMyJS;
    document.head.appendChild(script);
    // If you need to support IE8, use the following instead of the previous line:
    //document.getElementsByTagName("head")[0].appendChild(script);
    </script>
</body>

Another option is to use document.write. This sort of thing may be the last at-least-partially appropriate use of document.write during the main parsing of the page:

<head>
    <!-- head stuff here -->
</head>
<body>
    <!-- content here -->
    <script src="https://myurl.de/settingsFile.js"></script>
    <script type="text/javascript">
    document.write('<script src="' + SettingsFile.UrlToMyJS + '"><\/script>');
    </script>
    <script>
    myFunc();
    </script>
</body>
Answer:1

I need to initialize a Vue component's data with the result of an AJAX call. I tried the following: data: function () { return { supplierCount: 0 } }, created: function () { axios.get("/...

I need to initialize a Vue component's data with the result of an AJAX call. I tried the following: data: function () { return { supplierCount: 0 } }, created: function () { axios.get("/...

  1. initialize with data transfer
  2. initialize data from props vue
  3. initialize data in struct
  4. initialize data in java
  5. initialize data in orchestrator
  6. initialization with data
  7. initialization data in c++
  8. initialize dataframe with column names
  9. vue initialize data with function
  10. initialize dataframe with zeros
  11. vue initialize data with computed
  12. data initialization in oops
  13. vue js initialize data with props
  14. initialize arraylist with data
  15. initialize select2 with data
  16. initialize list with data java
  17. initialize list with data c#
  18. initialize dictionary with data c#
  19. datatable initialize with data
  20. initialize map with data java

I am stuck with ajax... I have a cart and it has gift vouchers, i) the code checks for valid voucher and if its not valid then it should show message "invalid voucher". ii) If voucher if valid and is ...

I am stuck with ajax... I have a cart and it has gift vouchers, i) the code checks for valid voucher and if its not valid then it should show message "invalid voucher". ii) If voucher if valid and is ...

  1. show message in view mvc
  2. show message in view laravel
  3. how to show message view in android studio
  4. show viewbag message in view
  5. show tempdata message in view
  6. show message from controller to view
  7. show error message in view mvc
  8. show error message in view from controller

JS models concurrency by an event loop. As a result there are no race conditions. So what are the drawbacks of the following type safe operation in the main scope of a program that would justify any ...

JS models concurrency by an event loop. As a result there are no race conditions. So what are the drawbacks of the following type safe operation in the main scope of a program that would justify any ...

  1. what is the typical class size at texas a&m
  2. what is the typical class size for ucla
  3. what is the typical class size at stanford university
  4. what is the typical class size at usc
  5. what are a typical class of anticancer drugs
  6. what is the typical class size at nyu
  7. what is the typical class size at texas tech
  8. what is the typical class size at asu
  9. what is the typical class size at unc
  10. what is the typical class size at gcu
  11. what is the typical class size at osu
  12. what is the typical class size at gsu
  13. what is the typical class size at pvamu
  14. what is the typical class size at wssu
  15. what is the typical class size at ttu
  16. what is the typical class size at csumb
  17. what is the typical class size at uc merced
  18. what is the typical class size at vsu
  19. what is the typical class size at apsu
  20. what is the typical class size at unr

I need to add class to input parent if input is not empty or focus because I want to change label position. <div class="input-field"> <input type="email" name="email" ng-model="email" ...

I need to add class to input parent if input is not empty or focus because I want to change label position. <div class="input-field"> <input type="email" name="email" ng-model="email" ...