The easiest way is to use the WebBrowser.InvokeScript method:
this.WebBrowser.InvokeScript("initialize", 1, 2);
Alternatively, you can also rewrite JavaScript code as follows:
function initialize(lat, log) { var mapProp = { center: new google.maps.LatLng(lat, log), zoom: 5, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); } document.myfunc = initialize;
So now you can access myfunc from C # code:
private void WebBrowser_OnLoadCompleted(object sender, NavigationEventArgs e) { dynamic document = WebBrowser.Document; document.myfunc(1, 2); }
You can also call myfunc without the dynamic keyword:
private void WebBrowser_OnLoadCompleted(object sender, NavigationEventArgs e) { var document = this.WebBrowser.Document; document.GetType().InvokeMember("myfunc", BindingFlags.InvokeMethod, null, document, new object[] {1, 2}); }
source share