Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript loading order causing issues

I have one JS file in the header (this is for Google DFP) and another before the </body>

I have found if the header JS file doesn't load before the bottom one I get this error in Chrome Console:

 Uncaught TypeError: Object #<Object> has no method 'defineSlot' 

The defineSlot is defined in the first script. This issue only occurs on around every 10 page refresh so most of the time its ok.

I want advise on how to deal with this issue. Below are the 2 scripts:

Header Script:

<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
    var gads = document.createElement('script');
    gads.async = true;
    gads.type = 'text/javascript';
    var useSSL = 'https:' == document.location.protocol;
    gads.src = (useSSL ? 'https:' : 'http:') + 
    '//www.googletagservices.com/tag/js/gpt.js';
    var node = document.getElementsByTagName('script')[0];
    node.parentNode.insertBefore(gads, node);
})();
</script>

<script type='text/javascript'>
googletag.cmd.push(function() {
    googletag.pubads().enableAsyncRendering();
    slot1 = googletag.defineSlot('/21848415/GoneGlobal_Square', [250, 250], 'doubleClickSquareIndex-250-250').addService(googletag.pubads());
    slot2 = googletag.defineSlot('/21848415/GoneGlobal_Skyscraper', [160, 600], 'doubleClickSkyscraperIndex-160-600').addService(googletag.pubads());
    slot3 = googletag.defineSlot('/21848415/GoneGlobal_Leaderboard', [728, 90], 'doubleClickLeaderboardIndex-728-90').addService(googletag.pubads());
    slot4 = googletag.defineSlot('/21848415/GoneGlobal_Leaderboard', [728, 90], 'doubleClickLeaderboardHeaderInternal-728-90').addService(googletag.pubads());
    slot5 = googletag.defineSlot('/21848415/GoneGlobal_SmallSquare', [200, 200], 'doubleClickSmallSquareHeaderExternal-200-200').addService(googletag.pubads());
    googletag.enableServices();
});
</script>

Below is the second script - this is only part of it (its quite long):

$(function() {
    ///////////////////////// Double Click AD Variables
    var slot1 = googletag.defineSlot('/21848415/GoneGlobal_Square', [250, 250], 'doubleClickSquareIndex-250-250');
    var slot2 = googletag.defineSlot('/21848415/GoneGlobal_Skyscraper', [160, 600], 'doubleClickSkyscraperIndex-160-600');
    var slot3 = googletag.defineSlot('/21848415/GoneGlobal_Leaderboard', [728, 90], 'doubleClickLeaderboardIndex-728-90');
    var slot4 = googletag.defineSlot('/21848415/GoneGlobal_Leaderboard', [728, 90], 'doubleClickLeaderboardHeaderInternal-728-90');
    var slot5 = googletag.defineSlot('/21848415/GoneGlobal_SmallSquare', [200, 200], 'doubleClickSmallSquareHeaderExternal-200-200');
)};

Is there a way to stop a script running another another method/function is loaded?

Can I create some sort of dependency?

How can I ensure the top JS is loaded before the bottom?

like image 455
Adam Avatar asked Dec 26 '22 18:12

Adam


1 Answers

The other solutions here might work but the real issue is to do with the way you are using DFP.

DFP is loaded asynchronously so the method defineSlot will not exist on your page at the time the script that is causing the error is executed.

All of your DFP code should be contained within the googletag.cmd.push wrapper which will store commands until DFP is loaded and then execute them in the correct order.

Simply wrapping any DFP code like so, should fix your errors:

googletag.cmd.push(function() {
    // Double Click AD Variables

});
like image 166
Matt Cooper Avatar answered Dec 29 '22 10:12

Matt Cooper