1

I've already lost many hours trying to do something I think it is impossible.

I have a script with a source to a website and that source is a simple link like:

<script type='text/javascript' src='http://ads1.qadabra.com/t?asdasdasdasd'></script>

This script only runs while the document is loading and all my efforts to try to run it after the document has loaded were ruined.

I wonder if I can place the src tag inside the document, will it run after the document has loaded? That script loads a banner from that website and I want to load it x seconds after the page has totally loaded.

Does anyone has a solution to this? I have already asked several questions about this issue in the past few days but I can't get to a solution :/

Thanks in advance!

This problem is the same as mine but does not offer good solutionshttps://forums.digitalpoint.com/threads/how-to-load-the-advertising-banner-code-after-the-website-has-been-fully-loaded.2286973/

5
  • 1
    api.jquery.com/jQuery.getScript Commented Dec 1, 2013 at 18:31
  • Does not work with a link. Commented Dec 1, 2013 at 18:33
  • $(document).ready(function(){}); for jQuery wrap that around your code in the link so it will run when the document is ready. Commented Dec 1, 2013 at 18:36
  • 1
    @Th3lmuu90 the jQuery .getScript() should do what you want. If you think it won't, you should explain why. Commented Dec 1, 2013 at 18:37
  • It wont work because scripts with sources cant be loaded after the document has loaded. Commented Dec 1, 2013 at 19:27

1 Answer 1

3

Using jQuery (i didn't test it):

$(document).ready(function(){

var delay = 5000;
setTimeout(
    function(){

        $.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {

        console.log( data ); // Data returned
        console.log( textStatus ); // Success
        console.log( jqxhr.status ); // 200
        console.log( "Load was performed." );
    });

}, delay);

});

From the docs:

http://api.jquery.com/jQuery.getScript/

The callback is fired once the script has been loaded but not necessarily executed.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes plus a possible setTimeout() around that to do it "x seconds after the document has loaded".
It wont work because scripts with sources cant be loaded after the document has loaded.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.