Skip to content

Instantly share code, notes, and snippets.

@MartinMystikJonas
Created May 24, 2018 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinMystikJonas/163edca770fbde561a4fae9e97d43acd to your computer and use it in GitHub Desktop.
Save MartinMystikJonas/163edca770fbde561a4fae9e97d43acd to your computer and use it in GitHub Desktop.
cookie
if(status == cookieconsent.status.deny) {
$('script[cookieconsent=enabled]').each(function() {
console.log("Cookie disabled: " + $(this).data("cookietype"));
$(this).remove();
});
} else {
$('script[cookieconsent=disabled]').each(function() {
scriptOrigin = $(this);
script = scriptOrigin.clone();
script.attr('type', 'text/javascript');
script.attr('cookieconsent', 'enabled')
console.log("Cookie enabled: " + script.data("cookietype"));
(document.getElementsByTagName('head')[0]).appendChild(script.get(0));
});
}
@MartinMystikJonas
Copy link
Author

MartinMystikJonas commented May 24, 2018

Example of script execute only on allowed cookies. Notice text-plain type to prevent automatic execution.

<script type="text/plain" cookieconsent="disabled" data-cookietype="Google Analytics">
--
var _gaq = _gaq \|\| [];
_gaq.push(['_setAccount', 'UA-5327682-24']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
 console.log("Google Analytics activated");
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment