- TLC Home Home
- Discussions Discussions
- Documentation Documentation
- Knowledge Base Knowledge Base
- Education Education
- Blog Blog
- Support Desk Support Desk
03-31-2016 07:44 AM
If we load utag.js async, is there an event that utag.js fires that tells us it has loaded.
03-31-2016 07:56 AM - last edited on 03-31-2016 10:44 AM by kathleen_jo
@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(); } } })();
03-31-2016 01:00 PM
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
Copyright All Rights Reserved © 2008-2023