3

I have a winforms application which is using Webbrowser control.

when I access a page this page has a code like this:

(document).ready(function(){
window.print();
}

but I do not want javascript execution on this page.

On webBrowser1_DocumentCompleted event I am trying to remove script elements from Document Text but could not find appropriate event, function etc..

Is there any workaround for this?

1
  • I hope you find my answer useful, this is a common question but not many people know about the method I've posted. Commented Sep 1, 2012 at 18:06

2 Answers 2

2

If you are having troubles with popups popping up, i've included a solution for you, and if you want to disable/enable javascript on the client machine (or even just read/query if it is enabled/disabled) ive included that answer for you as well, here we go:

Which popup message do you want to disable? If it's the alert message, try this, obviously resolving the window or frame object to your particular needs, I’ve just assumed top-level document, but if you need an iframe you can access it using window.frames(0). for the first frame and so on... (re the JavaScript part)... here is some code, assuming WB is your webbrowser control...

WB.Document.parentWindow.execScript "window.alert = function () { };", "JScript"

However, in your case, since you want to disable print, just swap .alert for .print.

You must run the above code only after the entire page is done loading, i understand this is very difficult to do (and a full-proof version hasn't been published yet) however I have been doing it (full proof) for some time now, and you can gather hints on how to do this accurately if you read some of my previous answers labelled "webbrowser" and "webbrowser-control", but getting back to the question at hand, if you want to cancel the .confirm JavaScript message, just replace window.alert with window.confirm (of course, qualifying your window. object with the correct object to reach the document hierarchy you are working with). You can also disable the .print method with the above technique and the new IE9 .prompt method as well.

Here is a Constant String (declared in VB) that will get rid of the 4 popups, including the new IE9 popup. In addition, it will get rid of the onbeforeunload event, and it will do all these on every single page its run on. If you have frames, you don't need to run them in frames as well, just running in parent document is enough. Here (declared in VB6), the _ is just fo rline continuation I placed there to make it easier to read, the & is for string concatenation, and the ; in javascript just means consider what follows as another line of code. This way, you can run many lines of code (below, I think 5) in one physical line.

Public Const DEFAULT_JS_TO_RUN_AFTER_EACH_PAGE_LOAD As String = _
& "window.alert = function () { }; window.confirm = function () { }; " _
& "window.prompt = function () { }; window.print = function () { }; " _
& "window.onbeforeunload = function () { };"

If you want to disable JavaScript entirely, you can use the registry to do this, and you must make the registry change before the webbrowser control loads into memory, and every time you change it (on & off) you must reload the webbrowser control out and into memory (or just restart your application).

The registry key is \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ - the keyname is 1400 and the value to disable it is 3, and to enable it is 0.

Of course, because there are 5 zones under the Zones key, you need to either change it for the active zone or for all zones to be sure. However, you really don't need to do this if all you want to do is supress js dialog popup messages.

Let me know how you go, and if I can help further.

Sign up to request clarification or add additional context in comments.

Comments

2

When you initialize the WebBrowser use this

webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate(url);

Hope this will work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.