We are using webbrowser control display the content. While loading the content first time it display an error message.
Error message
“You are currently runninginternet explorer7, which is not supported by….”
Observations
1. Webpage supported browser details.
2.Above mentioned error display in webbrowser control 1<sup>st</sup> time but when we try to load the same content again it will not display such type of error message.
3. To replicate the issue, Go to internet options – Delete browsing History - clear cookie and reload page via webbrowser control.
4. There is no property on the browser control which can be used to suppress this kind of error.
6. The page which we are trying to view is not maintained by us.
5. Code snippet to suppress such error message first time.
string DisableScriptError =
@"function noError() {
return true;
}
window.onerror = noError;";
var doc = (HTMLDocument)webInfoViewer.Document;
var head = doc.getElementsByTagName("head").Cast<HTMLHeadElement>().First();
var script = (IHTMLScriptElement)doc.createElement("script");
//script.text = "document.getElementById('messagearea').style.display = 'none';";
//script.text = DisableScriptError;
//script.text = "alert('hello')";
script.text ="window.onerror = true;";
head.appendChild((IHTMLDOMNode)script);
using wpf webbrowser control
PFA of toy sample to reproduce the issue.
Step to reproduce the issue.
Go to internet explorer – General tab -> browsing History -> click delete button -> launch delete browsing history dailog-> select “cookie” and click on delete button.
Try to run the log application.
It show’s an error message.
To reproduce the issue do the above mentioned steps again.
<UserControlx:Class="LogApplication.WebContentDisplayView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"mc:Ignorable="d"d:DesignHeight="300"d:DesignWidth="300"><Grid><WebBrowserName="webInfoViewer"LoadCompleted="webInfoViewer_LoadCompleted"Navigated="webInfoViewer_Navigated"Loaded="webInfoViewer_Loaded"></WebBrowser></Grid></UserControl>
publicpartialclassWebContentDisplayView:UserControl{stringInfoContentURI="http://www.ncbi.nlm.nih.gov/pubmed/24387772?tool=MedlinePlus";staticreadonlyGuid SID_SWebBrowserApp =newGuid("0002DF05-0000-0000-C000-000000000046");publicstaticreadonlyRoutedEventNewPageEvent=EventManager.RegisterRoutedEvent("NewPage",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(WebContentDisplayView));[ComImport,InterfaceType(ComInterfaceType.InterfaceIsIUnknown)][Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]internalinterfaceIServiceProvider{[return:MarshalAs(UnmanagedType.IUnknown)]objectQueryService(refGuid guidService,refGuid riid);}[ComImport,Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]privateinterfaceIOleServiceProvider{[PreserveSig]intQueryService([In]refGuid guidService,[In]refGuid riid,[MarshalAs(UnmanagedType.IDispatch)]outobject ppvObject);}publicWebContentDisplayView(){InitializeComponent();}privatevoid webInfoViewer_LoadCompleted(object sender,NavigationEventArgs e){BrowserSettings();}privatevoidBrowserSettings(){SHDocVw.IWebBrowser2 iWebBrowser2 =GetIWebBrowser2();if(iWebBrowser2 ==null)return;SHDocVw.DWebBrowserEvents_Event wbEvents =(SHDocVw.DWebBrowserEvents_Event)iWebBrowser2; wbEvents.NewWindow-=newSHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow); wbEvents.NewWindow+=newSHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);}publicSHDocVw.IWebBrowser2GetIWebBrowser2(){IServiceProvider serviceProvider =(IServiceProvider)webInfoViewer.Document;Guid serviceGuid = SID_SWebBrowserApp;Guid iid =typeof(SHDocVw.IWebBrowser2).GUID;return(SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid,ref iid);}privatevoidOnWebBrowserNewWindow(stringUrl,intFlags,stringTargetFrameName,refobjectPostData,stringHeaders,refboolProcessed){//string HeaderDetails = string.Empty;//HeaderDetails = "Accept = text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01" + "\n";//HeaderDetails = HeaderDetails + "Accept-Encoding=gzip, deflate" + "\n";//HeaderDetails = HeaderDetails + "Accept-Language = en-US" + "\n";//HeaderDetails = HeaderDetails + "User-Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0E; .NET4.0C)";//webInfoViewer.Navigate(Url,null,null,HeaderDetails); webInfoViewer.Navigate(Url);}privatevoid webInfoViewer_Navigated(object sender,NavigationEventArgs e){SetSilent(webInfoViewer,true);// make it silent RaiseNewPageEvent();//HideScriptingError();InjectDisableScript();}publicstaticvoidSetSilent(WebBrowser browser,bool silent){if(browser ==null)thrownewArgumentNullException("browser");// get an IWebBrowser2 from the documentIOleServiceProvider sp = browser.DocumentasIOleServiceProvider;if(sp !=null){Guid IID_IWebBrowserApp =newGuid("0002DF05-0000-0000-C000-000000000046");Guid IID_IWebBrowser2 =newGuid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");object webBrowser; sp.QueryService(ref IID_IWebBrowserApp,ref IID_IWebBrowser2,out webBrowser);if(webBrowser !=null){ webBrowser.GetType().InvokeMember("Silent",BindingFlags.Instance|BindingFlags.Public|BindingFlags.PutDispProperty,null, webBrowser,newobject[]{ silent });}}}voidRaiseNewPageEvent(){RoutedEventArgs newEventArgs =newRoutedEventArgs(WebContentDisplayView.NewPageEvent);RaiseEvent(newEventArgs);}privatevoid webInfoViewer_Loaded(object sender,RoutedEventArgs e){ webInfoViewer.Navigate(InfoContentURI);}publiceventRoutedEventHandlerNewPage{ add {AddHandler(NewPageEvent, value);} remove {RemoveHandler(NewPageEvent, value);}}privateconststringDisableScriptError=@"function noError() { return true; } window.onerror = noError;";privatevoidInjectDisableScript(){var doc = webInfoViewer.DocumentasHTMLDocument;if(doc !=null){//Create the sctipt element var scriptErrorSuppressed =(IHTMLScriptElement)doc.createElement("SCRIPT"); scriptErrorSuppressed.type ="text/javascript"; scriptErrorSuppressed.text =DisableScriptError;//Inject it to the head of the page IHTMLElementCollection nodes = doc.getElementsByTagName("head");foreach(IHTMLElement elem in nodes){var head =(HTMLHeadElement)elem; head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);}}}privatevoidHideScriptingError(){stringDisableScriptError=@"function noError() { return true; } window.onerror = noError;";var doc =(HTMLDocument)webInfoViewer.Document;//var tag = @"<meta http-equiv='X-UA-Compatible' content='IE=9' >";var head = doc.getElementsByTagName("head").Cast<HTMLHeadElement>().First();var script =(IHTMLScriptElement)doc.createElement("script"); script.text ="function test() { document.write('<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\"/>'); alert('hellow'); };"+" window.load = test(); ";//script.text = "document.getElementById('messagearea').style.display = 'none';";//script.text = DisableScriptError;//script.text = "alert('hello')";//script.text = "window.onerror = true;"; head.appendChild((IHTMLDOMNode)script);}}