Working on cleaning up my errors and depreciated web-pages,
I've run into this Warning Message:
jquery-3.4.1.js:9725
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
For more help, check https://xhr.spec.whatwg.org/.
I want to properly handle the JavaScript right:
- Keep the scripts and links in a self-contained area, i.e.
- Run after the page loads
But I don't know how to:
- Convert from inline JS references to JS-file
- What the internal JS format should look like
I've been using:
<head>
<script>$(document).ready(function () { $('<script/>', { type: 'text/javascript', src: 'assets/js/set-background.js' }).appendTo('head'); }); </script>
</head>
With the JS-file:
...
var body = document.getElementsByTagName("body")[0];
body.className += " change-" + idx;
...
The warning doesn't happen if the script is:
<body>
...
<script src="assets/js/set-background.js"></script>
...
</body>
And tried,
<head>
<script>$(function(){'assets/js/set-background.js'}) </script>
</head>
<head>
<script type="text/javascript" src="assets/js/set-background.js"></script>
</head>
<body class="container" onload="assets/js/set-background.js">
I've seen other developers discussions:
- javascript - How to run a function when the page is loaded? - Stack Overflow
- Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it - Stack Overflow
- javascript - $(document).ready equivalent without jQuery - Stack Overflow
- How to run a function in jquery - Stack Overflow
And I've also been reading: