2014-09-20 26 views
1

我在包含webbrowser控件的VS2012中編寫了一個小應用程序。我想加載一個包含javascript的html文件。 webbrowser控件無法處理javascript默認值。所以我一直在做一些閱讀。加載並調用包含javascript的html文件

我有以下的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文件?

    預先感謝任何建議:)

回答

0

它看起來像下面的代碼爲我工作:

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"); 
    } 
}