开发者

Canceling javascript requests in IE

does anyone know how to handle and cancel javascript requests in IE? Basically, i'm interested in such functionality for C# WebBrowser control. Th开发者_JS百科e goal here is to handle javascript/ajax requests to internet and if request's URL match some filter then cancel it. Think of it as some sort if ads blocker. But any solution will be fine because i think i will be able to use it somehow in my project. Or maybe you know how to do this with other browser/wrapper? I would appreciate any help.


If you are referring to ajax calls then it depends how you make the call. If you make the call using jquery for example then an example of how to cancel it is here:

Abort Ajax requests using jQuery

If you make the call directly using and XmlHttpRequest object then see the abort method here:

http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx

So basically you call the relevant kill/abort method, and then the handler or error callbacks that you have attached to the ajax call should never be called.

If you want to block ajax calls in a WebBrowser control there seems to be a way, but it's a bit messy. See the later part of this discussion:

http://social.msdn.microsoft.com/Forums/en/winforms/thread/c7c9a6f9-9875-4d8b-8c87-81f2c423fa6a

Basically you can modify the HtmlDocument concerned to take out the ajax calls in the first place. However, it does seem to be the case you can't disable javascript for a given IE session (so not just for the one in the WebBrowser control), it's a global setting.


Not sure what you're referring to by "JavaScript Requests" but... If you're using the Web Browser Control in .Net 3.5+ Framework, you could intercept the HTML sent to the control and scrape the JavaScript off, but that's fairly clugey.

If you're able to edit the JavaScript directly, you could overwrite the function. So if the function has been written as:

var alertUser = function(){alert('Hello, silly user.');}

You could write alertUser = function(){}; Which basically is setting it to an empty function so it won't error when it's called; but instead do nothing.

If you want a function to "cancel" you would just add a return;

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜