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>
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("/...
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 ...
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 ...
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" ...