简体   繁体   English

如何在WebBrowser控件中运行JavaScript?

[英]How can I make JavaScript run in WebBrowser control?

In my winforms aplication I have a WebBrowser control named webBrowser1. 在我的winforms应用程序中,我有一个名为webBrowser1的WebBrowser控件。

In code all I added is navigating to a page: 在代码中我添加的所有内容都是导航到页面:

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.Navigate("http://localhost:6489/Default.aspx");
}

The code for the page to which I navigate is: 我导航的页面的代码是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TableRowShow.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
      window.onload = function()
      {
        document.getElementById('addDestination').setAttribute('onclick', 'addDest();');
      }

      function attach()
      {
        document.getElementById('addDestination').setAttribute('onclick', 'addDest();');
      }

      var i = 1; // position of next tr to be shown

      function addDest()
      {
        var trs = document.getElementById('travelTable').getElementsByTagName('tr');

        if (trs[i] != null)
          trs[i++].style.display = "";
      }
    </script>
</head>
<body>
    <form id="form1" runat="server">
      <table id="travelTable">
        <tr>
          <td>
            <asp:TextBox runat="server" />
          </td>
        </tr>
        <tr style="display: none">
          <td>
            <asp:TextBox runat="server" />
          </td>
        </tr>
        <tr style="display: none">
          <td>
            <asp:TextBox runat="server" />
          </td>
        </tr>
      </table>
      <asp:HyperLink runat="server" ID="addDestination"
        ClientIDMode="Static"  NavigateUrl="javascript:;" >
        Add Destination
      </asp:HyperLink>
    </form>
</body>
</html>

Seen in a browser like IE or Chrome the page looks like this: 在IE或Chrome等浏览器中看到,页面如下所示:

Clicking on Add Destination anchor creates a new input: 单击“添加目标”锚点将创建一个新输入:

The problem that I'm having with WebBrowser control is that it loads the page but the JavaScript doesn't work. 我使用WebBrowser控件的问题是它加载页面但JavaScript不起作用。

If I click on Add Destination nothing happens, even though the same page works well in Chrome or IE. 如果我点击添加目的地没有任何反应,即使同一页面在Chrome或IE中运行良好。

Placing a breakpoint and using: 放置断点并使用:

webBrowser1.Document.InvokeScript("addDestination");

inside the Immediate window and then continuing to run the program activates the JavaScript in that function adding a new input. 在立即窗口内,然后继续运行程序激活该函数中的JavaScript添加新输入。

Thanks for replies! 谢谢你的回复!

尝试附加这样的点击处理程序,而不是在onloadattach函数中使用setAttribute

document.getElementById('addDestination').onclick = addDest;

您可以尝试将ScriptErrorsSuppressed属性设置为false以查看是否发生任何JavaScript错误。

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

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