Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The not-lazy-load ad loader needs to wait until jQuery.dfp is available #95

Open
2 tasks
benlk opened this issue Feb 5, 2019 · 0 comments
Open
2 tasks
Labels
Milestone

Comments

@benlk
Copy link
Collaborator

benlk commented Feb 5, 2019

Problem

This line of code:

<script type="text/javascript">
jQuery('.dfw-unit:not(.dfw-lazy-load)').dfp( window.dfw );
</script>

Can potentially be output upon the page before the script enqueues that load the js:

wp_localize_script( 'jquery.dfw.js', 'dfw', $data );
wp_enqueue_script( 'jquery.dfw.js' );

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
    $( document ).ready(function(){
    // Watch for scroll events, and determine whether any ads
    // need lazy loading. Wait a few seconds for images and text to render
    // and units to reach their "true" depth on the page.
    $( window ).on( 'scroll', lazyLoad );
    // Run lazyLoad once on DOM ready to load any ads in view
    lazyLoad();
    });
  • use asynchronous loading https://stackoverflow.com/a/8618519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant