Created
May 28, 2020 08:53
-
-
Save marcmascort/a63e26fcbdbca84e46b523c9729a7cbd to your computer and use it in GitHub Desktop.
Async Google Ads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Lazy ADs</title> | |
<style> | |
.adsbygoogle { | |
width:300px; | |
height:300px; | |
} | |
</style> | |
</head> | |
<body> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-XXXXXXXXX" | |
data-ad-slot="XXXXXXXXX" | |
data-ad-format="auto"></ins> | |
<script> | |
let example = { | |
scripts: [ | |
'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', | |
], | |
}; | |
</script> | |
<script src="https://code.jquery.com/jquery-3.5.1.min.js" defer></script> | |
<script src="main.js" defer></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
'use strict'; | |
function inject_script_to_head(url, id, async) | |
{ | |
let js, fjs = document.getElementsByTagName('script')[0]; | |
if (!id && document.getElementById(id)) | |
{ | |
return; | |
} | |
js = document.createElement('script'); | |
if (id) | |
{ | |
js.id = id; | |
} | |
if (async) { | |
js.defer = true; | |
} | |
js.src = url; | |
fjs.parentNode.insertBefore(js, fjs); | |
} | |
function load_lazy_scripts() | |
{ | |
let i; | |
if (example && example.scripts && typeof example.scripts === 'object') | |
{ | |
for (i in example.scripts) | |
{ | |
if (typeof example.scripts[i] === 'object') | |
{ | |
inject_script_to_head(example.scripts[i].url, example.scripts[i].id, example.scripts[i].async); | |
} else | |
{ | |
inject_script_to_head(example.scripts[i]); | |
} | |
} | |
} | |
} | |
function refresh_google_ads(init) | |
{ | |
if (init) | |
{ | |
try { | |
(adsbygoogle = window.adsbygoogle || []).push({ | |
google_ad_client: "ca-pub-XXXXXXXXX", | |
enable_page_level_ads: true | |
}); | |
} catch (err) { | |
// | |
} | |
} | |
let n = $('ins.adsbygoogle').not('[adsbygoogle-status="true"]').not('[data-adsbygoogle-status="true"]').not('[adsbygoogle-status="done"]').not('[data-adsbygoogle-status="done"]').length, i = 0; | |
for (i; i < n; ++i) | |
{ | |
try { | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
} catch (err) { | |
// | |
} | |
} | |
} | |
function ajax_insert_more_html(data) | |
{ | |
// Insert HTML to DOM | |
refresh_google_ads(); | |
} | |
jQuery(document).ready(function($) { | |
load_lazy_scripts(); | |
refresh_google_ads(true); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment