I want to remove this Google Analytics block, using jQuery.
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics/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.
THE PROBLEM
It works with .remove()
until you navigate away, then press back. Google Analytics hangs.
I want to remove this Google Analytics block, using jQuery.
<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.
THE PROBLEM
It works with .remove()
until you navigate away, then press back. Google Analytics hangs.
- 5 You can remove the elements, but I don't think this will do what you want...the script will have already run at that point. – Nick Craver Commented Jun 21, 2010 at 14:56
- Are you trying to validate form data? If so, this won't stop an attacker. Server-side validation is necessary. – ceejayoz Commented Jun 21, 2010 at 14:57
- No, I am creating some bespoke Javascript based screen-reader functionality. – Barrie Reader Commented Jun 21, 2010 at 15:00
- Does anyone have any idea? It's driving me insane!! – Barrie Reader Commented Jun 21, 2010 at 15:27
- 1 What is preventing you from removing the GA code directly from the source code? Is there a server side scripting language? – KatieK Commented Jun 21, 2010 at 17:22
4 Answers
Reset to default 7 +25Try 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!
You can also hook document.write and check if its google anlytics code before stopping it like this:
<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>
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
if (this.innerHTML.match(googleScriptRegExp))
Hope this is what you were looking for.
To actually remove the elements, jQuery('script:not([src^=http])').remove()
will work.