To actually remove the elements, jQuery('script:not([src^=http])').remove()
will work.
A:
Aaron Harun
2010-06-21 14:54:10
Nope, I'm afraid this doesn't work either >.<
Neurofluxation
2010-06-21 15:20:34
You mean from the "view source"? If so, there is no solution in JS. However, it does remove any script blocks without a `src` from the dom. If you want all removed, just use `jQuery('script').remove()`.
Aaron Harun
2010-06-21 18:04:40
@Aaron Harun - This removes all script tags though, I only want to remove a specific one...
Neurofluxation
2010-06-22 08:29:09
Can't you use their position (using eq())? This will only work of course if the scripts in the same order on every page.
pritaeas
2010-06-29 07:34:47
The problem isn't selecting the right script. By the time this can run the Google Analytics will already have been parsed and run. Ryano's solution above works because it loads the page, parses out the script, then essentially reloads the page with the parsed version.
jasongetsdown
2010-06-30 20:29:25
+4
A:
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!
Ryano
2010-06-24 11:14:17
@Ryano - Perfect, but this does remove EVERY instance of `document.write(unescape` but that's fine for what I need for the moment.
Neurofluxation
2010-06-24 11:15:47
A:
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>
Chris T
2010-06-30 22:43:10
A:
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.
spinon
2010-07-01 08:13:00