简体   繁体   中英

Call Silverlight method from Javascript when it's ready

I have a page that loads another window on button click. The loaded page has silverlight control on it, so it takes some time to load and get prepared before it can receive javascript calls.

What I need to do is to call a particular method of silverlight object right after the silverlight plugin gets loaded and is ready to interact with me.

Now, if the pop-up page was already opened then the code would be like that:

var slWin = window.open('PopupPage.html', 'WindowName');
var elem = slWin.document.getElementById('slControl');
elem.Content.SlObject.MethodA();

This works when the window is already opened because the control is already loaded and ready. I need to modify this code to handle the situation when the elem need some time to be prepared.

I tried to use jQuery's ready and load methods to add handlers to corresponding events, but with no particular lack. Here's the full snippet:

var slWin = window.open('', 'WindowName');

var elem = slWin.document.getElementById('slControl');
if (elem == null) {
    slWin.location.href = 'PopupPage.aspx';

    // this branch doesn't work
    $(slWin).load(function () {

        elem = slWin.document.getElementById('slControl');
        elem.Content.SlObject.MethodA();
    });
}
else {
    // this branch works fine
    elem.Content.SlObject.MethodA();
}

How do I solve this issue? I don't mind jQuery solutions.

This error is happening because the Silverlight object is not fully loaded when you are trying to access it.

Try to use the "onload" event of the silverlight object to dectect when it's ready to use. Here you have a link to the MSDN documentation:

http://msdn.microsoft.com/en-us/library/cc838107(v=vs.95).aspx

Hope it helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM