我正在尝试删除 html 中的这个脚本标记,它用于 Google 分析,我希望在使用时删除此脚本,不接受 cookie。
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46776072-1']);
_gaq.push(['_setDomainName', 'av.gov.mk']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl'
: 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
};
</script>
最佳答案
将 GA 脚本放入接受 cookie 后调用的函数中。
function startGA(source) {
document.getElementById(window.event.srcElement.id).style.display = "none";
alert("Google Analytics script will be rendered now!")
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46776072-1']);
_gaq.push(['_setDomainName', 'av.gov.mk']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type =
'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
}
<input type="button" id="acceptCookies" value="Accept Cookies" onclick="startGA(this)" />
关于javascript - 如何删除 html 内的 script 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571672/