我最近偶然发现了同样的问题。JavaScript 适用于 IE,但不适用于我的 C# webbrowser 组件。
解决方案是检查 HKLM\Security\Internet Explorer\MSHTML 注册表项。必须为 0 才能允许在 webbrowser 中使用 javascript!现在我的代码检查并将这个 reg 键更改为零(如果不是 0),然后调用 InitializeComponents()。
该键也将以另一种方式改变 webbrowser 的行为:箭头键现在不会将焦点从链接移动到链接,但它们会滚动 webbrowser 视图。
希望对你也有帮助。
编辑:这是一个代码示例:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WebBrowser
{
public partial class Form1 : Form
{
public Form1()
{
checkMSHTML(0);
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = false;
}
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.ImageIndex)
{
case 0:
webBrowser1.Url = new Uri( "http://192.168.128.5/www");
break;
case 1:
this.Close();
break;
}
}
/// <summary>
/// check and change MSHTML rendering engine
/// </summary>
/// <param name="iVal">0 = use new IE6 engine, enable JavaScript
/// 1 = use old PIE engine</param>
/// <returns></returns>
bool checkMSHTML(int iVal)
{
bool bRet = false;
Microsoft.Win32.RegistryKey rKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Security\Internet Explorer",true);
if (rKey != null)
{
int iMSHTML = (int) rKey.GetValue("MSHTML");
if (iMSHTML != iVal)
{
rKey.SetValue("MSHTML", iVal, Microsoft.Win32.RegistryValueKind.DWord);
rKey.Flush();
rKey.Close();
bRet = true;
}
else
{
rKey.Close();
bRet = true;
}
}
return bRet;
}
}
}