简体   繁体   English

WebBrowser控件和JavaScript错误

[英]WebBrowser control and JavaScript errors

When I access the page with the browser (ie9), the browser is rendering ok. 当我使用浏览器(ie9)访问页面时,浏览器呈现正常。

When I use the WebBrowser control I have JavaScript errors. 当我使用WebBrowser控件时,我有JavaScript错误。

I know I can suppress the scripts errors, but I want them to run correctly, because they affect the rendering and the functionality of the page. 我知道我可以抑制脚本错误,但我希望它们能够正确运行,因为它们会影响页面的呈现和功能。

How can I solve this problem ? 我怎么解决这个问题 ? Can I integrate IE9 directly in the Windows Form and use similar methods like with the WebBrowser control (navigate,get id, invoke click) ? 我可以直接在Windows窗体中集成IE9,并使用类似于WebBrowser控件的方法(导航,获取ID,调用点击)?

Thanks. 谢谢。

What I would do is assign an object to webbrowser.ObjectForScripting and then inject a javascript function that assigns windown.onerror to a wrapper that calls the external script in the host app. 我要做的是将一个对象分配给webbrowser.ObjectForScripting,然后注入一个javascript函数,将windown.onerror分配给一个调用主机应用程序中的外部脚本的包装器。 Like: 喜欢:

window.onerror = function(message, url, lineNumber) 
{ 
  window.external.errorHandler(message, url, lineNumber);
}

Refere to: http://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/ 参考: http ://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/

If you have IE9 installed, the WebBrowser will still use IE7 mode unless you override this behaviour with a registry setting - as described in this StackOverflow answer . 如果安装了IE9,WebBrowser仍将使用IE7模式,除非您使用注册表设置覆盖此行为 - 如此StackOverflow答案中所述 This is the most likely cause of the JavaScript errors you're getting in the WebBrowser (because you're not seeing the same errors in IE9). 这是您在WebBrowser中遇到的JavaScript错误的最可能原因(因为您在IE9中没有看到相同的错误)。

You can make the registry setting using the following c# code (which sets IE10 mode if Windows 8 is detected) and changing app-name.exe to match your own application. 您可以使用以下c#代码进行注册表设置(如果检测到Windows 8,则设置IE10模式)并更改app-name.exe以匹配您自己的应用程序。 You should add an error handler for the case where there are insufficient privileges (admin privileges are required to write to this registry key). 您应该为没有足够权限的情况添加错误处理程序(写入此注册表项需要管理员权限)。

string installkey = @"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string entryLabel = "app-name.exe";
System.OperatingSystem osInfo = System.Environment.OSVersion;

string version = osInfo.Version.Major.ToString() + '.' + osInfo.Version.Minor.ToString();
uint editFlag = (uint)((version == "6.2") ? 0x2710 : 0x2328); // 6.2 = Windows 8 and therefore IE10

RegistryKey existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, false); // readonly key

if (existingSubKey.GetValue(entryLabel) == null)
{
     existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, true); // writable key
     existingSubKey.SetValue(entryLabel, unchecked((int)editFlag), RegistryValueKind.DWord);
}

You can use the following code line to get rid of those types of errors: 您可以使用以下代码行来消除这些类型的错误:

webBrowser1.ScriptErrorsSuppressed = true;

It will prevent getting JavaScript errors. 它可以防止出现JavaScript错误。

So i know the post is old, but it was a recent problem for me and i had to do some serious digging and thinking outside the box. 所以我知道这篇文章很老,但这对我来说是一个最近的问题,我不得不在盒子外面进行一些认真的挖掘和思考。

basically like most replies here - you cannot alter the webbrowser control to use the most recent IE engine. 基本上像这里的大多数回复 - 你不能改变webbrowser控件使用最新的IE引擎。 mine uses IE7 by default, i have seen some replies that basically changes/ adds stuff to registry, am always not comfy when it comes to the registry, a cleaner way to address this issue would be to append code on your website that forces it to use the most current IE engine on any pc and works like a charm. 我默认使用IE7,我看到一些回复基本上改变/添加到注册表的东西,在注册表时总是不舒服,解决这个问题的一个更简洁的方法是在你的网站上附加强制它的代码在任何一台电脑上使用最新的IE引擎,就像一个魅力。

if you have access to the web.config file of the page you intend to browse, simple append: 如果您有权访问要浏览的页面的web.config文件,则简单追加:

    <system.webServer>
<httpProtocol>
    <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=edge" />
    </customHeaders>
</httpProtocol>
</system.webServer>

and your site would force webbrowser control to run the most current IE engine on your computer. 并且您的站点将强制webbrowser控件在您的计算机上运行最新的IE引擎。 Other options are found here: 其他选项可在此处找到:

https://www.leapinggorilla.com/Blog/Read/1016/ie-ate-my-css---disabling-compatability-mode https://www.leapinggorilla.com/Blog/Read/1016/ie-ate-my-css---disabling-compatability-mode

I should state that this would only work if you have access to the page / web.config of the website/ application you are trying to access- which was my case. 我应该声明,只有当您可以访问您尝试访问的网站/应用程序的page / web.config时,这才有效 - 这就是我的情况。

THe WebBrowser control uses IE7. WebBrowser控件使用IE7。 So if there is a problem then your script does not work for IE7 and you will have to fix that. 因此,如果出现问题,那么您的脚本不能用于IE7,您必须解决这个问题。

You cannot integrate IE9 as it depends on it being installed on the computer and not everyone has IE9 installed. 你不能集成IE9,因为它取决于它安装在计算机上,并不是每个人都安装了IE9。

As a help to whoever else may have this problem, I tried all these things and nothing worked for me. 作为对其他人可能有这个问题的帮助,我尝试了所有这些东西,但对我来说没有任何作用。 Here's what does work. 这是什么工作。 I am not sure exactly what causes this error, but apparently when you just press "F5" in VS to debug your app, it runs YourProject.vshost.exe as the process name. 我不确定究竟是什么导致了这个错误,但显然当你在VS中按“F5”调试你的应用程序时,它会运行YourProject.vshost.exe作为进程名称。 If you run the same app from the command line, it will show up as YourProject.exe, and the javascript errors vanish. 如果从命令行运行相同的应用程序,它将显示为YourProject.exe,并且javascript错误消失。 I think IE sees the app running visa via VSHOST and decides this is fishy and disables javascript from loading correctly. 我认为IE看到应用程序通过VSHOST运行签证,并决定这是可疑的,并禁用正确加载的JavaScript。

So... go into your project setting for your executable. 所以......进入你的可执行文件的项目设置。 Select "Debug" options. 选择“调试”选项。 Select "Start External Program". 选择“启动外部程序”。 Browse to and select Debug\\YourProgram.exe (NOT YourProgram.vshost.exe). 浏览并选择Debug \\ YourProgram.exe(NOT YourProgram.vshost.exe)。 Save, recompile, and hit F5. 保存,重新编译,然后点击F5。

Everything should work as per usual now, and Visual Studio even attaches to the process for you automatically. 现在一切都应该按照惯例工作,Visual Studio甚至会自动为您处理这个过程。

Enjoy! 请享用!

Grego Grego

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM