use window.open and window.close method in silverlight c#.
Introduction:This Blog introduced how to use window.open and window.close method in silverlight c#.
Using window.open method we can open new browser window and close window.
window.open Method :
1.The syntax of the window.open method is given below:
open (URL, windowName[, windowFeatures])
A> URL :The URL of the page to open in the new window. This argument could be blank.
B> windowName :A name to be given to the new window. The name can be used to refer this window again.
C> windowFeatures :A string that determines the various window features to be included in the popup window (like status bar, address bar etc)
Example 1>window.open ("http://www.google.com", "mywindow","location=1,status=1,scrollbars=1, width=100,height=100");
A window with location bar, status bar, scroll bar and of size 100 X 100.
Example 2>window.open ("http://www.google.com","mywindow","menubar=1,resizable=1,width=350,height=250");
A window with menu bar. The window is re-sizable and is having 350 pixels width and 250 pixels height.
Example 3>Moving the window to a desired location :
We can use the window.moveTo function to move the popup window to a desired location.
The code below shows positioning the popup at a desired location.
window.open("http://www.google.com", "mywindow", "location=1,status=1,scrollbars=1, width=100,height=100");
mywindow.moveTo(0, 0);
Example 4> On button click we can open new window using window.open method as follow:
private void btn_Click(object sender, RoutedEventArgs e)
{
HtmlPage.Window.Eval("window.open('http://www.google.com');");
}
To pass querystring to the URL :
public string Name;
private void btn_Click(object sender, RoutedEventArgs e)
{
string h = "window.open('http://www.google.com/finance?q=";
h = h + Name + "');";
HtmlPage.Window.Eval(h);
}
window.close Method :
The window.close () method could be used to close a window.
The close method closes only windows opened by JavaScript using the open method.
If you attempt to close any other window, a confirm message is displayed, asking the user to choose whether the window is to be closed or not.
If you have the reference to a window, you can use the reference to the window to close.
For example:
popup_window = window.open("");
popup_window.close ();
Example 1> On button click close the window :
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
HtmlPage.Window.Eval("window.close();");
}