1
private void WebBrowser_Clicked(object sender, RoutedEventArgs e)
{
  wb.Navigate("C:/Users/intern3/source/repos/MarketingProject/Samples/WPF/RESTToolkitTestApp/index.htm");  
  wb.InvokeScript("SetCoords", new object[] { coord1, coord2 });
}

wb is a WebBrowser object

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" http-equiv="X-UA-Compatible" content="IE=edge" />
    <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=AgCKDO35_SFD68BDTuy_QuFz48T2P3FmYQKttx8y2DXQNR-ufCeae5riWUYmPCmk' async defer></script>
    <script type='text/javascript'>
        var map;
        var lattitude = 34.2;
        var longitude = -117.8;
        function SetCoords(lat, long) {
            lattitude = lat;
            longitude = long;
        }
...

I've tried adding the coordinates to the end of the url as a querystring but since it's a local file I don't think it's possible. If it is please let me know because that would be an easy solution to this too. But otherwise, this is giving me this error:

System.Runtime.InteropServices.COMException: 'Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))'

1
  • Better use WebView2 nowadays. Commented Jul 6, 2022 at 7:28

1 Answer 1

3

It takes some time until the WebBrowser control has loaded the page so that it knows the Javascript function. Instead of calling the function directly after the load, use a handler for the LoadCompleted event to run the function, e.g.:

public MyForm() // This is the constructor of your form/page
{
  InitializeComponent();
  wb.LoadCompleted += WebBrowser_LoadCompleted;
}

private void WebBrowser_Clicked(object sender, RoutedEventArgs e)
{
 wb.Navigate("C:/Users/intern3/source/repos/MarketingProject/Samples/WPF/RESTToolkitTestApp/index.htm");  

}

private void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
  wb.InvokeScript("SetCoords", new object[] { coord1, coord2 });
}

Above code adds the handler for LoadCompleted manually, but you can also add it in the designer.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to do something like that(load completed event handler) on the Javascript file? Right now I'm resorting to a Refresh button to update the coordinates that I'm sending.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.