Javascript function call from Silverlight

I am trying to call a javascript function (in our code) from a silverlight control. I am trying to call a function via:

HtmlPage.Window.Invoke("showPopup", new string[] { "http://www.example.com" });

and I get the error "Could not call: showPopup"

I can call HtmlPage.Window.Invoke("alert", new string[]{"test"}); no problem, but not its own function.

I can also open this page in the IE developer tools and manually call showPopup("http://www.example.com") , and it works as expected.

Thus, the js function works, and the Silverlight binary can find other js functions. What am I missing here?

Additional notes:

  • The function is called in the button click event handler, so this happens after the page (and script) loads.
+7
javascript silverlight
source share
6 answers

Yeah! I understood. Our application uses iframe, so the displayed html looks something like this:

 <html> <head></head> <body> Stuff <iframe> <html> <head></head> <body>Other Stuff</body> </html> </iframe> <body> </html> 

And the Silverlight control in question is in an iframe. The problem was that the file containing the showPopup function was referencing an external <head> (why could I call this function using the IE toolbar), but not an internal <head> . Adding a file link to an in-iframe <head> solved the problem.

Deviate from disappointment, but thanks for all the help.

+2
source share

Is the showPopup javascript function in the same html or aspx page as the Silverlight control? Usually you get the error "Failed to Invoke ..." if the javascript function does not exist:

 HtmlPage.Window.Invoke("functionThatDoesNotExist", new [] { "Testing" }); 

alt text http://www.freeimagehosting.net/uploads/3ed38e3ce8.png

Which browser do you use when you get this problem?

Are you using the latest version of Silverlight?

Do you use the ScriptableType attribute anywhere?

Is it possible to list the code for a short but complete program that causes this problem on your computer ...

+2
source share

In fact, linking to the script again from the iframe is not the most efficient way of referencing the code contained in the parent. If your function is called showPopup, you can paste it into your iframe:

 <script type="text/javascript"> var showPopup = parent.showPopup; </script> 

And voila. The explanation for this is that all the "global" functions and objects are part of this "global namespace" ... which is a "windowed" object. Therefore, if you are trying to access "global" functions from a child, you need to either call the parent function (for example, parent.showPopup ("....")) or declare a local alias for it (which is what we do in the above above example).

Hooray!

+2
source share

Make sure your script is fully loaded before trying to call functions from it.

0
source share

This is how I do it. But I create Silverlight without a visual studio. I just have raw html, xaml and js (javascript). Note the MouseLeftButtonUp and the value "LandOnSpace"

  <Canvas x:Name="btnLandOnSpace" Background="LightGreen" MouseLeftButtonUp="LandOnSpace" Cursor="Hand" Canvas.Top ="0" Width="70" Height="50"> <TextBlock Text="LandOnSpace" /> </Canvas> 

 function LandOnSpace(sender, e) { //on server if (!ShipAnimateActive && !blnWaitingOnServer) { blnWaitingOnServer = true; RunServerFunction("/sqgame/getJSLLandOnSpace"); ShowWaitingBox(); }; else { alert('Waiting on server.'); }; 

}

0
source share

I had the same problem in VS 2010 with SL 4. I created several methods and put them in one JS file. However, this file was not added to the main section of the ASPX file. Adding it solved the problem. The difference is that although I did not have a separate header section in the iframe, I had a problem and it was resolved.

0
source share

All Articles