最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing a script tag with a specific content using jquery - Stack Overflow

programmeradmin1浏览0评论

I have a page that i dont have access to its an obvius site. I would like to remove a script html tag with a content. For now i have this but is not working. I am using userscripts like coding!

function main(){
var def = $('script[type="text/javascript"]').html();
$('script[type="text/javascript"]').each(function() {
if (def == 'document.write("<scr"+"ipt type=\'text/javascript\' src=\'/"+(new Date()).getTime()+"/3e155555e1b26c2d1ced0f645e_1_1.js\'></scr"+"ipt>")')
$('script[type="text/javascript"]').remove();
                                                    }
            }

UPDATE:

<script type="text/javascript">document.write("<scr"+"ipt type='text/javascript' src='/"+(new Date()).getTime()+"/3e1a0cd37f25a6e1b26c2d1ced0f645e_1_1.js'></scr"+"ipt>")</script>

This is the whole script what i want to remove... it inserts a div that i am removing right now i just wanted to know if there is any other method. BUt as i see the only is the hosts file thing :)

I have a page that i dont have access to its an obvius site. I would like to remove a script html tag with a content. For now i have this but is not working. I am using userscripts like coding!

function main(){
var def = $('script[type="text/javascript"]').html();
$('script[type="text/javascript"]').each(function() {
if (def == 'document.write("<scr"+"ipt type=\'text/javascript\' src=\'http://storing./javascripts/"+(new Date()).getTime()+"/3e155555e1b26c2d1ced0f645e_1_1.js\'></scr"+"ipt>")')
$('script[type="text/javascript"]').remove();
                                                    }
            }

UPDATE:

<script type="text/javascript">document.write("<scr"+"ipt type='text/javascript' src='http://somedomain./javascripts/"+(new Date()).getTime()+"/3e1a0cd37f25a6e1b26c2d1ced0f645e_1_1.js'></scr"+"ipt>")</script>

This is the whole script what i want to remove... it inserts a div that i am removing right now i just wanted to know if there is any other method. BUt as i see the only is the hosts file thing :)

Share Improve this question edited Sep 14, 2012 at 14:44 rsz asked Sep 14, 2012 at 14:20 rszrsz 1,1611 gold badge18 silver badges38 bronze badges 4
  • 2 Removing a script tag will not undo what it has already done. – Joseph Silber Commented Sep 14, 2012 at 14:23
  • If the script has already loaded you're almost certainly too late to prevent whatever it's supposed to do to the page. – Alnitak Commented Sep 14, 2012 at 14:23
  • Can you explain why you want to remove the script? Maybe we can help with a workaround... – Ian Commented Sep 14, 2012 at 14:25
  • p.s. when you call .html() you get the contents of the element, not the element itself. – Alnitak Commented Sep 14, 2012 at 14:27
Add a ment  | 

2 Answers 2

Reset to default 5

I don't believe this will work, since a loaded script will already have run.

That said, you probably want something like this:

$('script').each(function() {
    if (this.src.substring(0, 31) === 'http://storing./javascripts/') {
        $(this).remove();
    }
});

It's impossible to match the <script> tag based on the output of .html() because that only returns the contents of the element, and not the outer <script> element nor the element's attributes.

When a script is loaded in a page, it is evaluated and executed by the browser immediately after. After the script has been executed, the content of the script tag is irrelevant.

You might be able to achieve what you want by unbinding the events which might have been loaded by the script. Are there any events you want to disable?

If the script is in a certain domain and you want to block all traffic to it, you could add the following entry to your hosts file:

127.0.0.1    storing.

This will prevent the request to reach it's destination.

发布评论

评论列表(0)

  1. 暂无评论