Do something after an external script is fully loaded via jQuery append

Asked

Viewed 307 times

-1

I have a <script> external being added to the page via jQuery com:

$("body").append("<scr"+"ipt src='url_do_script.js'></scr"+"ipt>");

This script adds some elements on the page (some tags). The script works perfectly, but it is asyronous when loading the page, that is, the page loads normally without waiting for what the script will return. But I need to manipulate some tags returned by this .js as soon as they are available.

The problem is that I haven’t found a way to know when this script has already been fully loaded on the jQuery page.

With pure Javascript I can know by adding the script via document.createElement and appendChild. Just make a .onload:

var e = document.createElement("script");
e.src = "url_do_script.js";
document.body.appendChild(e);

e.onload = function(){
   // o script foi totalmente carregado
}

How to do something with the .append jQuery? I’ve tried other ways .get, .load and .ajax but does not work due to CORS (Cross-origin Resource sharing).

I tried to put a onload="funcao()" in the <script> and it didn’t work either.

Any idea how to do this or is there another way using jQuery other than with .append?

  • The fact of onload does not work, is that the element script does not have this event. When you use with createElement, he is creating an object supporting that event, not necessarily creating the element itself. He has tried it with the getScript? You can also create extend the jquery

1 answer

1


I was able to resolve the matter with the tip from @Valdeir Psr. Just add the getScript shortly after the append that the code recognized that the elements were loaded on the page:

$("body").append('<scr'+'ipt src="url_do_script.js"></scr'+'ipt>');
$.getScript( "url_do_script.js", function() {
   // fazer algo após o script ter sido carregado
});

Did not show CORS error or anything. It worked perfectly.

UPDATING

Truth that making a append and using getScript, the script was being called 2 times.

How the tag needs to exist <script> complete on the page (<script src="url_do_script.js"></script>) otherwise it does not work in this specific case, because it still possesses a id (<script id="id_qualquer" src="url_do_script.js"></script>) and is based on this id that the script works, I informed the problem as follows:

$("body").append('<script id="id_qualquer"></scr'+'ipt>');
$("#id_qualquer").attr("src","url_do_script.js");
$.getScript("url_do_script.js")
.done(function(){
   // fazer algo após o script ter sido carregado;
});

I added the src of script via id and executed on getScript after the .done. Worked perfectly.

  • 2

    It makes no sense to use append and getScript at the same time, you are having the file uploaded twice.

  • @bfavaretto Truth. Although it was working, I checked via console that it was returning 2 times a script information. I made the above modifications and now it works without duplicity. Obg!

  • But keep loading twice. See the "network" tab of your console. Probably one of the two requests will come from the cache, but it’s still weird to leave the code that way. If your script tag has an id, you can take this ID by JS and treat the upload with a onload of this element, more or less as you showed in the question.

  • @bfavaretto I tested before, really was ordering twice as it returned a 2x error. After the change, only returned 1x and managed to change what I wanted. Well, I tried by onload with a function onload="funcao()" and did not even enter the function. It is because it is a banner advertising service and only works that way. The programming of . js of them requires there to be the tag on the page with the specific id and everything else, otherwise it doesn’t work. This way I did, the script has no effect when placing src separately. Only has an effect on getScript. Thanks!

  • @bfavaretto to sum up, what I did was just build the tag... the execution takes place in getScript. Abs!

  • http://jsbin.com/dajakilufa/edit?html,output

  • @bfavaretto Com document.write browser blocks access to script.

  • Then try with append, but without the getscript

  • @bfavaretto I’ve tried everything rs... with append the onload didn’t work. As I said, don’t call the function in onload.

Show 4 more comments

Browser other questions tagged

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