Highlighted

If utag.js is loaded asynchronously, is there an event that fires when utag.js is loaded.

Silver Contributor
Silver Contributor

If we load utag.js async, is there an event that utag.js fires that tells us it has loaded.

2 REPLIES 2
Highlighted

If utag.js is loaded asynchronously, is there an event that fires when utag.js is loaded.

Bronze Contributor
Bronze Contributor

@steve_sherwood I also ran into this same problem -- I needed to be sure that utag was loaded before running my own custom script that relied on utag.  Here's the code I am using:

 

// append tealium tag and include a check so that we know when utag is loaded and ready for use in other scripts
		(function(a,b,c,d){
			a='//tags.tiqcdn.com/utag/mycompany/main/prod/utag.js';
			b=document;c='script';d=b.createElement(c);d.src=a;d.type='text/java'+c;d.async=true;
			a = b.getElementsByTagName(c)[0]; a.parentNode.insertBefore(d, a);

			// uses onload, for modern browsers
			d.onload = function() {
			    if (! d.onloadDone) {
			        d.onloadDone = true;
			        runMyScriptThatReliesOnUtag();
			    }
			};
// uses onreadystatechange, for IE9 d.onreadystatechange = function() { if ( ( "loaded" === d.readyState || "complete" === d.readyState ) && ! d.onloadDone ) { d.onloadDone = true; runMyScriptThatReliesOnUtag(); } } })();

 

 

If utag.js is loaded asynchronously, is there an event that fires when utag.js is loaded.

Employee Emeritus

@steve_sherwood @juji555

 

If you're interested in another solution you can also use the following. Please note, that it requires running jQuery on the site. The benfits of this snippet are that it's a little shorter and you don't have to check for IE. 

 

jQuery.getScript( "https://tags.tiqcdn.com/utag/my_account/my_profile/prod/utag.js" )
  .done(function( script, textStatus ) {
    runMyScriptThatReliesOnUtag();
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log( "fail" );
});

You don't really need the check for fail, unless you want it. For additional information about the jQuery getScript method, see the documentation: jQuery getScript