C#Winform調用網頁中的JS方法

來源:互聯網
上載者:User

其實還是還是相當的簡單,本文將詳細的用代碼來展示一下如何調用,並且送上原始碼。

這個主題,其實我是在QQ問問裡看到的,那為Q友送上了380分,所以就寫做了這樣的一個DEMO。同時這裡也分享給更多正在尋找與此相關的朋友的。

C#代碼:

EXFCODE:

查看源碼

列印關於

01

02
public partial class Form1 : Form

03
{

04
public Form1()

05
{

06
InitializeComponent();

07
}

08
private void Form1_Load(object sender, EventArgs e)

09
{

10
button4_Click(null, null);

11
}

12
//顯示普通網頁

13
private void button4_Click(object sender, EventArgs e)

14
{

15
webBrowser1.Navigate("http://www.exfsoft.com/demo/qqww1229/exf.htm");

16
}

17
//執行普通網頁的JS方法

18
private void button1_Click(object sender, EventArgs e)

19
{

20
try

21
{

22
webBrowser1.Document.InvokeScript("doSave", new object[] { "Winform普通:" });

23
}

24
catch { }

25
}

26
//顯示架構頁

27
private void button5_Click(object sender, EventArgs e)

28
{

29
webBrowser1.Navigate("http://www.exfsoft.com/demo/qqww1229/main.htm");

30
}

31
//執行架構頁中的JS方法

32
private void button2_Click(object sender, EventArgs e)

33
{

34
try

35
{

36
webBrowser1.Document.Window.Frames["mainFrame"].Document.InvokeScript("doSave", new object[] { "Winform架構:" });

37
}

38
catch { }

39
}

40
}

網頁代碼,由於是測試了兩種模式,所以這裡用了兩個網頁。

子網頁:exf.htm

EXFCODE:

查看源碼

列印關於

01
<html>

02
<head>

03
<title>exfosft</title>

04
<meta name="author" content="玄峰" />

05
<meta name="QQ" content="4513427" />

06
<script type="text/javascript">

07
function doSave(type)

08
{

09
alert(type + "執行的該事件。");

10
}

11
</script>

12
</head>

13
<body>

14
<form name="form1" id="form1">

15
<table style="width:100%;">

16
<tr>

17
<td onclick="doSave('WEB')" style="cursor:pointer;"><img src="save.png" align="absMiddle"> 儲存</td>

18
</tr>

19
</table>

20
</form>

21
</body>

22
</html>

架構頁:

EXFCODE:

查看源碼

列印關於

1
<html>

2
<head>

3
<title>exfsoft.com</title>

4
</head>

5
<frameset rows="10%,50%"

6
<frame name=top src="http://www.exfsoft.com"

7
<frame name="mainFrame" src="exf.htm"

8
</frameset>

9
</html>

運行效果為:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.