Windows Mobile 6.5 Professional

我怕爱的太早我们不能终老 提交于 2019-11-29 17:22:50

I recently stumbled upon the same issue. JavaScript works in IE but not in my C# webbrowser component.

The solution was to check HKLM\Security\Internet Explorer\MSHTML registry key. It must be 0 to allow javascript inside webbrowser! Now my code checks and changes this reg key to zero (if not alreday 0) and then calls InitializeComponents().

The key will change the behaviour of the webbrowser in another way too: the arrow keys now do not move the focus from link to link but they scroll the webbrowser view.

Hope that helps you too.

EDIT: here is a code sample:

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;
    }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!