Th3lmuu90
Th3lmuu90

Reputation: 1717

javascript src inside script

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/

Upvotes: 1

Views: 1165

Answers (1)

Fernando
Fernando

Reputation: 7895

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.

Upvotes: 3

Related Questions