C# 窗体 webbrowser 窗体调用javascript方法 实例

来源:互联网 发布:手机能不能做淘宝客服 编辑:程序博客网 时间:2024/05/22 12:37

///////窗体文件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

namespace MyApplication

{

    public partial class MyForm : Form

    {

        public MyForm()

        {

            InitializeComponent();

        }

 

        private void MyForm_Load(object sender, EventArgs e)

        {

            string path = System.Windows.Forms.Application.ExecutablePath;

            FileInfo exeInfo = new FileInfo(path);

            path = exeInfo.DirectoryName + "\\";   //程序根目录

            string file_name = path + "html_file\\myscript.html"; //html带路径文件名 html文件放在debug目录下得html_file文件夹中

            MyBrowser.Url = new Uri(file_name);

        }

 

        private void MyButton_Click(object sender, EventArgs e)

        {

            object[] parameters = new object[2];

            parameters[0] = 3;

            parameters[1] = 2;

            MyBrowser.Document.InvokeScript("Add", parameters);//如果javascript方法不带参数 则MyBrowser.Document.InvokeScript("函数名");即可

        }

    }

}

/////////////////////////////////////////////////页面文件//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<html>

<head>

<script type="text/javascript">

function Add(a,b)

{

alert(a+"+"+b+"="+(a+b));

}

</script>

</head>

</html>

0 0
原创粉丝点击