locked
Passing values to a javascript function from a webbrowser control RRS feed

  • Question

  • Hi,

    I am passing values from webbrowser control on a winform to a javascript function.

     WebBrowser1.Document.InvokeScript("GetMap", New Object() {CObj(addr), CObj(addr2)})

    Now, in the javascript, do i have to convert the values from object to a string to use them?

    This is my javascript code:

     function GetMap(primAddr,secnAddr)
             {
                map = new VEMap("myMap");
                map.LoadMap();
    //            Location=new Array("address1" , "address2");  - Working good
    //            Location=new Array(primAddr,secnAddr);   -----  Failing

     var Location=new Array()
    //            Location[0] = primAddr - Fails
    //            Location[1] = secnAddr - Fails

                try{
                alert(Location[0]); 

                 alert(Location[1]);
                GetRouteMap(Location);
                 }
                catch (e) {
                 alert(e.name + "TestLocation1234" + e.message);
                }

    Thanks

    Friday, March 19, 2010 3:47 PM

All replies

  • no. Javascript is a weakly typed language. You can treat same variable in different forms without having to do explicit conversions.
    Thanks

    #define sizeof(x) rand()
    Friday, March 19, 2010 3:55 PM
  • Omie,

    If i hardcode my values in this line
    Location=new Array("address1" , "address2");  - Everything works good.

    If i use the parameters, it is failing.
    Location=new Array(primAddr,secnAddr);
      alert(Location[0]);  -------- This shows the values properly.
      alert(Location[1]);

    So, i do nto understand what is goign wrong here.
    Hardcoding gives me proper result  and passing parameters fails(eventhough they have the values that i passed).

    Do you have an idea what i am missing here?

     

    Friday, March 19, 2010 4:11 PM
  • Hi rowter,

    Did the parameter comes from .Net code(like C#)? If so, I think you need to convert the arguments to the correct type and then pass them to the InvokeScript method.

    You can also pass all the parameters as strings and convert them in the java script code:
    http://msdn.microsoft.com/en-us/library/ms970723.aspx

    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, March 23, 2010 8:58 AM
  • Hi Aland Li,

    I am working with Bing Maps on a windows form. I am using a webbrowser control to host my form.
    From the webbrowser on my winform, my code looked like this.

    WebBrowser1.Document.InvokeScript("GetRouteMap", New Object() {CObj(addr), CObj(addr2)})
    WebBrowser1.Document.InvokeScript("GetMap", New String() {addr, addr2})// I tried all different formats here.

    Now in my javascript:

    <body onload="GetMap(primAddr, secnAddr);" style="font-family:Arial">
          <div id;='directions'></div>
       <div id='myMap' style="position:relative; width:400px; height:400px;"></div>
    </body>

    function GetMap(primAddr,secnAddr)
             {
                map = new VEMap("myMap");
                map.LoadMap();
                   //Location = new Array("myaddress1", "myaddress2"); --------------------If i hardcode the values here, the whole thing works.

                   alert("primAddr value is a type of :   " + typeof(primAddr));
                   alert("secnAddr value is a type of :   " + typeof(secnAddr));
                   //Location = new Array(a,b);

                   Location = new (primAddr, secnAddr);
                  try{
       alert(Location[0]); ------------------------- Has the values that i passed from my form
                      alert(Location[1]); ------------------------- Has the values that i passed from my form
                GetRouteMap(Location); ----------------Calling this function and passing the array.
                 }
                catch (e) {
                 alert(e.name + "TestLocation1234" + e.message);
                }
              }
        

    function GetRouteMap(Location)
             {
                 var options = new VERouteOptions;
                 //options.DrawRoute      = true;
                 options.RouteCallback  = ShowTurns;
    //             options.ShowDisambiguation = false;
                 options.DistanceUnit   = VERouteDistanceUnit.Mile;
                 var testroute = new VERoute;
                 alert(typeof(Location));

                 try{
                            alert(Location[0]);  -------------------  Has the values that i passed from my form
                            alert(Location[1]);  -------------------  Has the values that i passed from my form
                     map.GetDirections(Location, options);            -------------Here this function is expecting an array so, i pass the array directly. 

                         }
                        else{
                        alert("Location is not an array");
                        }
                        map.GetDirections(Location, options);
                    }
                catch (e) {
                 alert("GetRouteMethod Erorr:" + e.name + " " + e.message);
                          }
             }

     

    This is failing for some reason. If i hardcode the address values it is working good. But, if i send the parameters, it is failing. Is there any mistake in my code? Not sure why this is erroring out for the parameters. I checked the parameters ad they are of string type.

    Any idea what might be wrong?

    Tuesday, March 23, 2010 2:30 PM