在我的WinForms aplication我有一個名爲webBrowser1 WebBrowser控件。我怎樣才能讓JavaScript的web瀏覽器中運行的控制?
在代碼中的所有我添加被導航到的網頁:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://localhost:6489/Default.aspx");
}
用於向我瀏覽頁面中的代碼是:
<%@ 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>
見於如IE或鉻的頁面的瀏覽器看起來是這樣的:
點擊添加目標錨創建一個新的輸入:
,我要和WebBrowser控件遇到的問題是,它加載頁面,但JavaScript不工作。
如果我點擊添加目標沒有任何反應,即使在同一個頁面效果很好Chrome或IE瀏覽器。
放置一個斷點,並使用:
webBrowser1.Document.InvokeScript("addDestination");
立即窗口內,然後繼續運行該程序啓動的JavaScript在該函數中增加一個新的輸入。
感謝您的回覆!
解決了這個問題。任何ideea爲什麼它不起作用使用setAttribute,因爲在其他瀏覽器中它工作?我在Windows中使用IE8,它的工作原理。不WebBrowser也使用IE8(最新的IE安裝)? –