Javascript 使用 jQuery 删除 <script> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3085710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove <script> tags using jQuery
提问by Barrie Reader
I want to remove this Google Analytics block, using jQuery.
我想使用 jQuery 删除这个 Google Analytics 块。
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
//var pageTracker = _gat._getTracker("xxx");
pageTracker._trackPageview();
} catch(err) {}
</script>
THE REASON
原因
Because I am creating a bespoke screen reader convertor for jQuery based on a client specification. It's the Google Analytics that is bugging me.
因为我正在根据客户端规范为 jQuery 创建一个定制的屏幕阅读器转换器。困扰我的是谷歌分析。
THE PROBLEM
问题
It works with .remove()until you navigate away, then press back. Google Analytics hangs.
它可以工作,.remove()直到您离开,然后按回。谷歌分析挂起。
采纳答案by Ryano
Try this:
尝试这个:
var replacementDoneIn = $(document.body).text(); //remove Google Analytics document.write line
var regExMatch = /document\.write\(unescape/g;
var replaceWith = "//document.write";
var resultSet = replacementDoneIn.replace(regExMatch, replaceWith);
$("body").html(resultSet);
Hope that helps!
希望有帮助!
回答by Christopher Tarquini
You can also hook document.write and check if its google anlytics code before stopping it like this:
您还可以在停止之前挂钩 document.write 并检查其 google anlytics 代码,如下所示:
<script>
// Must run before google analytics though
old_document_write = document.write;
document.write = function(str)
{
if(/* determine if the str is google analyic code */)
return false; // dont write it
else
old_document_write(str);
}
</script>
回答by spinon
So this work as you hope. At least I think it will:
所以这如你所愿。至少我认为它会:
<script type="text/javascript">
$(document).ready(function () {
$("script").each(function () {
if (this.innerHTML.length > 0) {
var googleScriptRegExp = new RegExp("var gaJsHost|var pageTracker");
if (this.innerHTML.match(googleScriptRegExp) && this.innerHTML.indexOf("innerHTML") == -1)
$(this).remove();
}
});
});
</script>
Just to explain. I loop through all script tags on the page. If their innerHTML property has a length greater than 0 I then check the innerHTML of the script and if I find the string var gaJsHost or var pageTracker in it. I then make sure that I also don't see innerHTML in it as our script will obviously have these in it. Unless of course you have this code in a script loaded on the page using src in which case this script would not have an innerHTML property set and you can change the if line to just be
只是为了解释。我遍历页面上的所有脚本标签。如果他们的innerHTML 属性的长度大于0,我然后检查脚本的innerHTML,如果我在其中找到字符串var gaJsHost 或var pageTracker。然后我确保我也没有在其中看到innerHTML,因为我们的脚本显然会在其中包含这些。当然,除非您在使用 src 在页面上加载的脚本中包含此代码,在这种情况下,此脚本不会设置 innerHTML 属性,您可以将 if 行更改为
if (this.innerHTML.match(googleScriptRegExp))
Hope this is what you were looking for.
希望这就是你要找的。
回答by Aaron Harun
To actually remove the elements, jQuery('script:not([src^=http])').remove()will work.
要实际删除元素,jQuery('script:not([src^=http])').remove()将起作用。

