Visual C# - Getting rid of "Script Error" when using WebBrowser - c#

Ok so I have a WebBrowser called wb. And I load a page into wb using wb.Navigate("url"); When the page loads I get a box saying "Script Error".
Here is the one I get
How can I keep this from popping up in my program?

Not sure on newer browser control, but off the top of my head, there is an option called SuppressScriptErrors or something like that. Set that to true in your code before performing work. You'll also have to make sure that under Internet Options that debugging web pages is off (if you're getting the dialog above, it is off).
http://www.dev102.blogspot.com/2007/12/how-to-suppress-disable-script-errors.html
wb.ScriptErrorsSuppressed = true;

The method ScriptErrorsSuppressed referenced above is not supported in the webBrowser control in .Net 6 and higher.
webBrowser1.ScriptErrorsSuppressed = true;
we need to build a handler and override the default script error window.

Related

C# webbrowser control causes flash.ocx error on application exit

I've built a webbrowser using C# with the webbrowser control.
However every time a page is loaded which contains flash content the application will give an error on exit.
It states that the application may not have worked properly and flash.ocx is missing.
If users don't have flash player it's okay to not load the flash elements.
So the actual question i have is: Is it possible for me to hide/cancel/prevent this error?
You will need to extend the Webbrowser control class.
Perfect article, that helps you blocking these errors, preventing these errors and editing these errors:
http://www.codeproject.com/Articles/13598/Extended-NET-2-0-WebBrowser-Control

access the newwindow inside the newwindow event

I use a webbrowser control in my application to get data from a specific web page. This web page won't work with the older IE because it specifically checks for the IE version. So I made a registry change that allows my application to work as IE 9 and everything is ok most of the time...
The problem is when a newwindow has to be opened. It won't display anything. I guess that the newwindow is acknowledged as IE 7 and I don't know how to make it disguise itself as IE9.
I also tried the other way round. I thought that if I intercepted the newwindow url then I could just send it to IE9 or open it in another instance of a webbrowser control. But the newwindow event only allows to cancel the event. You can't get any useful information out of it.
I believe that interop services is what I need but I know nothing about them.
So I've got two questions:
(1) Can I make the newwindow identify as IE9? (and how...)
(2) How can I get the newwindow url using interop services (or anything, I wouldn't care)?
It is quite strange (from my perspective) that the WebBrowser control doesn't surface the much-more-useful NewWindow3 event.
This CodeProject article describes a remarkably simple way to make it available.
In the NewWindow event, assuming your first Wb control is named WB1 and the one you want to redirect to is WB2, do the following in your WB1 NW event.
Processed = True ' This cancels the current request.
WB2.Navigate URL ' This redirects it to the second WB2 control.
Otherwise, if you want to use the NW2 (NewWindow2) event instead of the NewWindow (NW) event, do this in the NW2 event of the WB1 control.
Set ppDisp = WB2.object ' Just swaps the objects around to redirect, don't need to issue a cancel.
Also, you can do this via BeforeNavigate2 (of WB1). But slightly different code.
Cancel = True ' Cancel Request.
WB2.Navigate2 URL ' Reissue it to WB2.
Now, as long as you control where it redirects to, you can get the new window URL easily, using WB2.LocationURL or Wb2.Document.URL if i am not mistaken.
Also, if you want to change the rendering engine to IE9 (even if IE9 is installed on your computer, WB control will use IE7 rendering engine for compatibility)... there are articles online and answers on SO (including some of my previous answers) which clarify how you can alter the registry to ensure the rendering engine used by the WB control is the same as that of the installed version (IE9), otherwise, it will always use IE7. And, if you have IE4, 5 or 6 installed on a machine, it will always use IE4 for the rendering engine. I think they update teh rendering version after ever 3-4 version changes. I'm assuming during version 10, WB control rendering version will be version 10 as well.
Let me know if you need more assistance with it and i've love to know how you got along and if this helped answer your question. All my examples are in VB6, but you can transform them easily.
Cheers.

How to stop javascript errors from opening windows in WebBrowser

I have a webBrowser control that is used in the backend to navigate some sites. This is never actually shown to the user.
However, some sites have broken javascript and that causes a window to pop-up with the continue script or stop. The page still works, but I don't want this window to show up.
Besides removing the javascript at runtime from the page, is there anything else I can do to suppress them?
Managed to answer my own question, found on another forum.
webBrowser.ScriptErrorsSuppressed = true;

Is there a way to track Internet Explorer Error messages from the WebBrowser Control?

Is there a way to track error messages in a webBrowser control? For example, in Firefox or Internet Explorer, I can look at the console output for Javascript errors. Is there a way to track those errors from within the application?
There are two related Microsoft KB articles:
"How to handle script errors as a WebBrowser control host"
"Script error notification is not sent to Exec method of WebBrowser Host"
Although these are related to unmanaged C++ code, you should be able to adopt these for the C# web browser control, too.
Another article suggests to subscribe to the onerror event:
m_htmlDoc = (IHTMLDocument2)axWebBrowser1.Document;
HTMLWindowEvents2_Event onErrorEvent;
onErrorEvent = (HTMLWindowEvents2_Event)m_htmlDoc.parentWindow;
onErrorEvent.onerror += new
HTMLWindowEvents2_onerrorEventHandler(myHTMLWindowEvents2_onerror);
And finally, there is a similar SO posting related to Delphi.
use window.onerror event, something like
window.onerror=function(msg, url, linenumber){
alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)
return true
}
Later versions of IE have a developer toolbar but sadly its not quite as good as Firebug or Web Inspector but thankfully we do have Firebug lite (http://getfirebug.com/firebuglite) which can help out with most things.

WebBrowser's ScriptErrorsSuppressed not letting me see a page that requires authentication

If you create a C# project (I'm using .NET Framework 4.0), add a WebBrowser, set ScriptErrorsSuppressed to true and navigate to http://vifprogram.com/community/ (which requires the kind of authentication that makes dialog popup for you to enter your credentials), you will get a "This program cannot display the webpage" error.
Any idea why? I tried to replicate this with Internet Explorer by switching the "Disable script debugging" in the internet options, but it works OK whether is checked or unchecked. Is there anything in IE that corresponds to WebBrowser's ScriptErrorsSuppressed? If I could at least replicate it in IE I could file a bug or something.
ScriptErrorsSuppressed suppresses not only errors, but also popup boxes such as the authentication box you refer to.
Here is an excerpt from MSDN:
When ScriptErrorsSuppressed is set to true, the WebBrowser control hides all its dialog boxes that originate from the underlying ActiveX control, not just script errors. Occasionally you might need to suppress script errors while displaying dialog boxes such as those used for browser security settings and user login. In this case, set ScriptErrorsSuppressed to false and suppress script errors in a handler for the HtmlWindow.Error event. For more information, see the code example in this topic.
http://msdn.microsoft.com/en-GB/library/system.windows.forms.webbrowser.scripterrorssuppressed.aspx

Categories