Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

javascript - Remove <script> tags using jQuery

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...