开发者

How to prevent from downloading and running script?

I wonder if it is possible in JavaScript to prevent <script src="unwantedscript.js" type="text/javascript"></script> object from running?

I ne开发者_开发百科ed to make it like it never existed. Is there an event where I can prevent from executing a script (based on src argument)?

I'm targeting Internet Explorer 8 but solution for other browsers are also welcomed (though it won't help me in anything ;)).

And please no JQuery or other libraries. I'm writing in C++ an IE add-on but I can use interfaces from MSHTML to 'use' JavaScript.


In IE (MSHTML) it's simple. You just 404 the HTTP request to get unwantedscript.js.


Trying to block a free hosting websites ads? If you do that your site can be deleted by the hosting company so watch out.

There is really nothing you can do about it other than wrap it in conditional comments

<!--[if IE 5]>
    <script src="foo.js"></script>
<![endif]-->

but if it is being injected into the page, you probably can not wrap it.


Since you (apparently) cannot inject a 404 error code for scripts, here's another idea: Use a custom security provider and reject the .js URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜