Skip to content

Commit 2bee212

Browse files
committed
Added scriptManager object to ensure scripts load in the correct order
1 parent 3729c51 commit 2bee212

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

examples/web/async.html

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,54 @@
2020
<!-- Snowplow starts plowing -->
2121
<script type="text/javascript">
2222

23+
window.scriptManager = (function() {
24+
25+
scriptQueue = [];
26+
27+
function enqueueScript(src, namespace) {
28+
scriptQueue.push([src, namespace]);
29+
if (scriptQueue.length === 1) {
30+
dealWithNextScript();
31+
}
32+
}
33+
34+
function dealWithNextScript() {
35+
if (scriptQueue.length > 0) {
36+
var newScript = document.createElement('script');
37+
var initialScript = document.getElementsByTagName('script')[0];
38+
newScript.async = 1;
39+
newScript.onload = function() {
40+
scriptQueue.shift();
41+
dealWithNextScript();
42+
}
43+
newScript.onerror = function() {
44+
scriptQueue.shift();
45+
dealWithNextScript();
46+
}
47+
newScript.src = scriptQueue[0][0];
48+
initialScript.parentNode.insertBefore(newScript,initialScript);
49+
}
50+
}
51+
52+
return {
53+
enqueueScript: enqueueScript
54+
}
55+
56+
}());
57+
2358
;(function(p,l,o,w,i,n,g) {
2459
p['GlobalSnowplowNamespace'] = p['GlobalSnowplowNamespace']||[];
2560
p['GlobalSnowplowNamespace'].push(i);
2661
p[i] = p[i] || function() {
2762
(p[i].q = p[i].q||[]).push(arguments);
2863
};
2964
p[i].q = p[i].q || [];
30-
n = l.createElement(o);
65+
scriptManager.enqueueScript(w, i);
66+
/*n = l.createElement(o);
3167
g = l.getElementsByTagName(o)[0];
3268
n.async = 1;
3369
n.src = w;
34-
g.parentNode.insertBefore(n,g);
70+
g.parentNode.insertBefore(n,g);*/
3571
}(window, document, 'script', '../../dist/snowplow.js', 'new_name_here'));
3672

3773
window.new_name_here('newTracker', 'primaryTrackerNamespace', 'd3rkrsqld9gmqf.cloudfront.net');
@@ -66,7 +102,7 @@
66102
<script type="text/javascript">
67103

68104
function playMix() {
69-
alert("Playing a DJ mix"); console.log(window.new_name_here.q)
105+
alert("Playing a DJ mix");
70106
window.new_name_here('trackStructEvent', 'Mixes', 'Play', 'MRC/fabric-0503-mix', '', '0.0');
71107
}
72108
function addProduct() {

0 commit comments

Comments
 (0)