javascript - 如何删除 html 中的脚本标签
问题描述
我正在尝试删除 html 中的这个脚本标签,它用于谷歌分析,我希望在使用不接受 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)" />
推荐阅读
- wordpress - 如何在我的 wordpress 子主题中覆盖任何插件
- html - HTML5 视频底部有深色渐变,不适合容器
- node.js - 查找创建帐户的时间
- macos - 使用第一行文本批量重命名 .txt 文件 - MacOS
- python - 在 Python 中删除文件时出现错误“进程无法访问文件”
- ansible - 在 Ansible 中将文件复制到主机
- php - php的分号错误
- macos - 如何在应用程序后台运行或锁定屏幕开启时保持 macOS 应用程序进程运行
- javascript - 自定义传单弹出滚动条
- typescript - 如何在本机反应中过滤功能组件中的状态数组