Calling Silverlight Code from Javascript in IE

I have a Silverlight application on an HTML page. The SL plugin is located in the HTML "object" tag. When a user closes a webpage, I want to call a function inside my Silverlight application. The code looks something like this:

(simplified) HTML:

<div id="silverlightControlHost"> <object id="MyApp" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/MyApp.xap" /> ... </object><iframe id="_sl_historyFrame"></iframe></div> 

Javascript Code:

 <script type="text/javascript"> window.onbeforeunload = confirmExit; function confirmExit() { var control = document.getElementById("MyApp"); var message = control.content.BrowserIntegration.MyAppFunction(); if (message) { return message; } } </script> 

It worked, and it no longer works, at least in Internet Explorer 8. The content property of the HTML object (control.content) is undefined. It’s strange. Has a variable recently occurred for this property? I am sure that he worked 2 months ago, and that he worked well long before that. The website is located on my trusted sites, and I did not find the settings in IE that I have changed since (maybe one or two, but, scanning the list, I did not find anything).

When I do a test with the "onLoad" event of the HTML object, the content is already undefined.

I do not know if the anomaly is a fact that it worked before or that it does not work now. But if someone can tell me how to make it work now, it will make my day.

+4
source share
3 answers

These operations did not solve my problem:

  • Virus Detection and Removal
  • Reduced Security in IE8
  • Reinstall Internet Explorer 8
  • Reinstalling Silverlight Component

Here is what solved my problem:

  • Reinstalling Silverlight Runtime

Note. I am using MS Visual Studio 2010 for .NET development

0
source

I am creating a wizard to call Silverlight code from IE:

1) You need to define a silverlight control: add an id tag:

 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" id="_sl_facebookapp"> 

2) It is required to register a scripted object from Silverlight code:

 HtmlPage.RegisterScriptableObject("FBSHandler", this); 

3) Mark the required silverlight method as ScriptableMember:

 [ScriptableMember] public void FBAuthorized(string authKey){} 

4) You must use a registered script object to call the Silverlight method:

 var silverlightCtrlId = '_sl_facebookapp'; function handleStatusResponse(response) { var control = document.getElementById(silverlightCtrlId); var accessToken = response.authResponse.accessToken; if (control != null) { control.Content.FBSHandler.FBAuthorized(accessToken); } } } 

It works great. Hope this helps you.

0
source

I think it is a matter of time. Your code to access the Content element may be executed before the Silverlight component is fully loaded.

I had the same problem and resolved it using the Javascript Timer until the item was fully loaded. Interestingly, the problem was only in IE and not in any of the other browsers.

0
source

Source: https://habr.com/ru/post/1414741/


All Articles