准备好后从 Javascript 调用 Silverlight 方法
我有一个页面,可以在单击按钮时加载另一个窗口。加载的页面上有 silverlight 控件,因此需要一些时间来加载并做好准备,然后才能接收 javascript 调用。
我需要做的是在 silverlight 插件加载并准备好与我交互后立即调用 silverlight 对象的特定方法。
现在,如果弹出页面已经打开,那么代码将如下所示:
var slWin = window.open('PopupPage.html', 'WindowName');
var elem = slWin.document.getElementById('slControl');
elem.Content.SlObject.MethodA();
当窗口已经打开时,这会起作用,因为控件已经加载并准备就绪。我需要修改此代码来处理 elem
需要一些时间准备的情况。
我尝试使用 jQuery 的 ready
和 load
方法将处理程序添加到相应的事件,但没有特别缺乏。这是完整的片段:
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();
}
如何解决这个问题?我不介意 jQuery 解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生此错误的原因是,当您尝试访问 Silverlight 对象时,它尚未完全加载。
尝试使用 silverlight 对象的“onload”事件来检测它何时可以使用。这里有 MSDN 文档的链接:
http: //msdn.microsoft.com/en-us/library/cc838107(v=vs.95).aspx
希望有帮助。 :)
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. :)