Skip to content

Commit d6933a7

Browse files
authored
Merge pull request #7670 from readthedocs/davidfischer/out-of-order-scripts
Fix for out of order script loading
2 parents e4f85cc + 30282aa commit d6933a7

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

readthedocs/core/static-src/core/js/doc-embed/sponsorship.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function inject_ads_client() {
1919
script.src = "https://media.ethicalads.io/media/client/beta/ethicalads.min.js";
2020
script.type = "text/javascript";
2121
script.async = true;
22+
script.id = "ethicaladsjs";
2223
document.getElementsByTagName("head")[0].appendChild(script);
2324
}
2425

@@ -185,6 +186,14 @@ function init() {
185186
// Ad client prevented from loading - check ad blockers
186187
adblock_admonition();
187188
adblock_nag(placement);
189+
} else {
190+
// The ad client hasn't loaded yet which could happen due to a variety of issues
191+
// Add an event listener for it to load
192+
$("#ethicaladsjs").on("load", function () {
193+
if (typeof ethicalads !== "undefined") {
194+
ethicalads.load();
195+
}
196+
});
188197
}
189198
},
190199
error: function () {

readthedocs/core/static-src/core/js/readthedocs-doc-embed.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ var sphinx = require('./doc-embed/sphinx');
77
var search = require('./doc-embed/search');
88

99

10-
$(document).ready(function () {
11-
footer.init();
12-
sphinx.init();
13-
// grokthedocs.init();
14-
// mkdocs.init();
15-
search.init();
16-
sponsorship.init();
17-
});
10+
// While much of this script relies on jQuery (which Sphinx relies on),
11+
// we purposefully do not use it before DOMContentLoaded in case scripts are loaded out of order
12+
(function () {
13+
function domReady(fn) {
14+
// If the DOM is already done parsing
15+
if (document.readyState === "complete" || document.readyState === "interactive") {
16+
setTimeout(fn, 1);
17+
} else {
18+
document.addEventListener("DOMContentLoaded", fn);
19+
}
20+
}
21+
22+
domReady(function () {
23+
footer.init();
24+
sphinx.init();
25+
search.init();
26+
sponsorship.init();
27+
});
28+
}());

0 commit comments

Comments
 (0)