Mutual invocation of child windows, parent windows, and Silverlight _javascript tips

Source: Internet
Author: User
Tags silverlight
In fact, the call between them is still very simple ~
Basic articles
one, the parent page calls in the child page JS
Let's create a subform first
Copy Code code as follows:

VAR win
$ (function () {
Root = $ (' #Root '). Val ()//Set the root directory
});
function Openplayer (ID, type, add) {
This is used to restrict the same window that only pops up once
In fact, even without the restriction, as long as the name in the window.open parameter is the same, it will not open repeatedly
The parameters in window.open can refer to other articles, there are many
if (win = null | | win.closed) {
Win = window.open (' http://loaclhost/sl.aspx ', ' win ', ' width=870,height=650,top=1,left=0,scrollbars=0,resizable=0, Status=1 ', true);
}

So how do you call it?
Copy Code code as follows:

This.alert (' Hello world! ');
Win.alert (' Hello world! ');
Win.onload = function () {This.alert (' test ');

As we all know, the first line is to pop a prompt box in this window (this can be omitted generally ~)
Well, actually, this and win point to the same type, different objects.
So, their methods are the same.
The second line is the code that pops a prompt box in a child window.
You can also write the child form event in the parent form ~
The third line is that when the subform is loaded, the parent window pops up the prompt box.
second, the subform calls the parent form
This is similar to the above, when the parent form opens a subform, you can access the parent form like this
Window.opener
This object is the parent form, directly calling the corresponding letter Number can
Three, subform call Silverlight Function or object
This is a detailed introduction to the MSND, Chinese ~ Here, I help you to mention the main steps (the method of invoking the Silverlight object is not posted, there are)
Additional transfer doors are attached:
1, now in Silverlight, add a attribute--scriptablemember to the function that needs to be called
Copy Code code as follows:

MyScriptableManagedType.cs
Using System.Windows.Browser;
Namespace HB {
public class MyScriptableManagedType {
[Scriptablemember ()]
public string Mytoupper (String str) {
Return str. ToUpper ();
}
[Scriptablemember ()]
public string Name {get; set;}
}
}

2. Register this object in the constructor
Copy Code code as follows:

MyScriptableManagedType SMT = new MyScriptableManagedType ();
Htmlpage.registerscriptableobject ("Myslapp", SMT);
If this function is not a function in the outer class, but a function of the SL principal, then the direct pass to the this pointer is OK.

3, open the page where slverlight
In the Silverlight object tag, add the OnLoad attribute with the value "pluginloaded".
The following example shows the HTML markup used for the Silverlight control reference.
Copy Code code as follows:

<object data= "Data:application/x-silverlight-2,"
Type= "Application/x-silverlight-2" width= "100%" height= "100%" >
<param name= "source" value= "clientbin/hb.xap"/>
<param name= "onerror" value= "Onsilverlighterror"/>
<param name= "OnLoad" value= "pluginloaded"/>
<!--...-->
</object>

4, in the existing script element on the page, add the JavaScript function of the pluginloaded method.
Use the GetHost method to get a reference to the Silverlight control. The following example shows the JavaScript code for the pluginloaded method.
Copy Code code as follows:

<script type= "Text/javascript" >
var slctl=null;
function pluginloaded (Sender,args) {//HTML version
Slctl = Sender.gethost ();
Alert (SlCtl.Content.mySLapp.MyToUpper ("Test string"));
}
</script>

four, Silverlight call JS in the subform
Also, the article on MSDN ~ Portal:
This super simple, direct:
HtmlPage.Window.Invoke ("methodname", args);
The previous argument is the function name, and the following argument is the parameter that this function needs to pass in
Improve the article
when the parent page opens the subpage, pass the parameters directly to Silverlight
This is primarily used when initializing
In the first part, we open the Http://loaclhost/sl.aspx window.
At this point, we can pass some parameters in the get way, for example: http://loaclhost/sl.aspx?id=1
This is then accessed in Silverlight:

String id = htmlpage.document.querystring["id"];

Six, how can the window front?
The Silverlight application in my subpage is actually a music player, and then the subpages are generally minimized.
However, when I pop the MessageBox in the Silverlight application it is still minimized ... This is very unfriendly to the user.
You can only call the Win.focus () function in the parent window if you want the front window to be placed.
But we just have to deal with it a little bit, in a parent window, in a child window, in SL.
So you can do this:
1. Write a function in the parent window, called Focus ()
Then call Win.focus ();
2, in the child window to write a function, called focus ()
Call Window.opener.Focus ()
3. Finally, if you want the Silverlight application to be predecessor, you call the focus () function in a parent window or a child window, or you can then call the focus () function of the child window in Silverlight

Seven, how to judge Silverlight has been loaded completed?
Here are a few ideas, there are right mistakes
(√) in the Silverlight application's constructor, calling the child page or the parent page (through the subpage) JS "This method is feasible, but slightly troublesome"
(x) Write window.onload event in subpages "Silverlight does not necessarily load complete when this event is triggered"
(√) Remember the third part, this in the 3rd step? <param name= "OnLoad" value= "pluginloaded"/> "Recommended Practices"
After adding this, the function function name that triggers the child page after Silverlight loading is complete can be changed
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.