Loading script asynchronously

Asked

Viewed 493 times

0

I have an administrative system using adminLTE. In the side menu I load all my scripts asynchronously (at least it was expected). However when I load an HTML asynchronously, and inside this file I load an external javascript, it generates me the following warning:

jquery.min.js:2 [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/.

Follow the code I’m using to load the files

$.ajax(url+"teste").done(function(data) {
    $("#ajax-content").html(data);
    $('#loadingMenu').remove(); // Retira o gif de loading
});

The file I carry is this:

<div class="box">
    <h1>HTML carregado assincronamente</h1>
</div>
<script src="meuScript.js"></script>

Placing the javascript code directly in the HTML file does not generate any error.

<div class="box">
    <h1>HTML carregado assincronamente</h1>
</div>
<script>
   alert('agora não gera aviso');
</script>

How do I load this script without having to put it on the same HTML page?

  • You’re confusing synchronous with asynchronous!

1 answer

3


The message says the opposite of what you understood;

What is being discontinued (has become obsolete) is the SYNCHRONOUS (that crashes the browser many times), for example this is synchronous:

xhr.open("GET", "/foo/bar", false);

And this is asynchronous:

xhr.open("GET", "/foo/bar", true);

In this answer I explain what is Ajax and Sjax:

Of course the above code is pure JS, but jQuery internally uses Javascript.

However as @Valdeirpsr said, it’s a problem in function jQuery.parseHTML (and/or dependencies), used internally in the function $(seletor).load("...");

In other words, it is a bug in jQuery, and it is likely that soon they will fix.

  • Yes, I went to take a closer look and noticed that I confused synchronous and asynchronous. I’m using a newer version in adminLTE. I changed my code that loads the page and am now using $.ajax("test"). done(Function(data) { Alert(); }); Even using the ajax function is giving the msg

  • edited the question adding more information. If you can take a look

  • I edited again. If it is not clear enough I will try to rephrase another question.

  • I put exactly the code that generates the problem. adminLTE has nothing to do with the problem. I only talked about it to give a sense of how I am working the loading of the content. The code is exactly that. I have a script that when I click on a button it runs this ajax function, bringing the html as return.

  • @Phelipe if you "debug" the code, you will see that the answer is correct. O jQuery makes this request synchronously. In the version 3.2.1 you can debug on the line 9497. The value of the property options.async, will be false.

  • And even if you use $.ajax({url: "index3.html", async:true}), the meuScript.js will be synchronously loaded by default. So use <script>$.getScript('meuScript.js');</script> on the other HTML page to load it asynchronously.

  • @Guilhermenascimento does not work. I tested with async and defer. The jQuery ignores these attributes.

  • @Phelipe edited the answer, is a jQuery BUG even, the way is to wait they solve, but for now your script will work, the message is only a warning.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.