【问题标题】:load and invoke html file containing javascript加载并调用包含 javascript 的 html 文件
【发布时间】:2014-11-14 16:05:59
【问题描述】:

我正在 VS2012 中编写一个包含 webbrowser 控件的小应用程序。我想加载一个包含 javascript 的 html 文件。 webbrowser 控件无法处理 javascript 默认值。所以我一直在做一些阅读。

我有以下 html 文件:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {
    var centerlatlng = new google.maps.LatLng(45.046006, -105.245867);
    var myOptions = {
        zoom: 13,
        center: centerlatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
</script>
</head>
<body style="margin:0px; padding:0px;">
<div id="map_canvas" style="width: 100%; height: 100%;"></div>
</body>
</html>

位于我的 c# 项目 (VS2012) 的调试目录中的 html 文件。

我想加载这个 html 文件并调用初始化函数。我一直在寻找关于如何做到这一点的清晰教程,但没有任何成功。据我所知,我尝试了以下方法。但是,并没有达到预期的效果。

代码:

maps_browser.DocumentText = File.ReadAllText("GPSmap.html");
maps_browser.Document.InvokeScript("initialize");

所以我的问题是:

  1. 如何将包含 javascript 的 *.html 文件加载到网络浏览器中?

  2. 如何从 html 文件中调用 javascript 函数?

  3. 如何正确导航到html文件?

    提前感谢您的任何建议:)

【问题讨论】:

    标签: c# javascript visual-studio webbrowser-control


    【解决方案1】:

    看起来下面的代码对我有用:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            maps_webbrowser.DocumentText = File.ReadAllText(@"GPSmap.html");
        }
    
        private void maps_webbrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Debug.WriteLine(maps_webbrowser.DocumentText.ToString());
            maps_webbrowser.Document.InvokeScript("initialize");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多