Skip to content

Commit

Permalink
More robust dynamic injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonitao committed Sep 15, 2016
1 parent cc4f38b commit 450731e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jquery.dfp.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,15 @@
};

gads.src = 'https://www.googletagservices.com/tag/js/gpt.js';
// See https://www.stevesouders.com/blog/2010/05/11/appendchild-vs-insertbefore/

This comment has been minimized.

Copy link
@venkatkarun

venkatkarun Sep 15, 2016

Simpler to just use document.getElementsByTagName('head')[0].appendChild(gads); IMO. The if-else seems unnecessary.

var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
if (node) {
node.parentNode.insertBefore(gads, node);
} else {
// Sometimes there is no script tag due to dynamic injection,
// and then we fallback to a simpler method.
document.documentElement.firstChild.appendChild(gads);
}

// Adblock plus seems to hide blocked scripts... so we check for that
if (gads.style.display === 'none') {
Expand Down

0 comments on commit 450731e

Please sign in to comment.