doubleclick-for-wp
doubleclick-for-wp copied to clipboard
The not-lazy-load ad loader needs to wait until jQuery.dfp is available
Problem
This line of code:
https://github.com/INN/doubleclick-for-wp/blob/bf4fcb5423ff359af955948ba253a8e2211185a3/inc/class-doubleclick.php#L210-L212
Can potentially be output upon the page before the script enqueues that load the js:
https://github.com/INN/doubleclick-for-wp/blob/bf4fcb5423ff359af955948ba253a8e2211185a3/inc/class-doubleclick.php#L180-L181
On one site in recent memory, the normal code output on the page was thus:
<script type='text/javascript' src='https://example.org/wp-content/plugins/doubleclick-for-wp/js/vendor/jquery.dfp.js/jquery.dfp.min.js?ver=0.3.0'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var dfw = { /* snip */};
/* ]]> */
</script>
<script type='text/javascript' src='https://example.org/wp-content/plugins/doubleclick-for-wp/js/jquery.dfw.js?ver=0.3.0'></script>
<script type="text/javascript">
jQuery('.dfw-unit:not(.dfw-lazy-load)').dfp( window.dfw );
</script>
but when that site loaded the https://wordpress.org/plugins/autoptimize/ plugin, the call to jQuery().dfp(); came before the concatenated scripts were loaded on the page.
Solution
- [ ] move that code into jquery.dfw.js code https://github.com/INN/doubleclick-for-wp/blob/f73e00d5cf8897fd75706453986b21a0e6e4a8a2/js/jquery.dfw.js#L45-L53
- [ ] use asynchronous loading https://stackoverflow.com/a/8618519