admin管理员组

文章数量:1023795

I want to use an external browser window to implement a preview functionality in a silverlight application. There is a list of items and whenever the user clicks one of these items, it's opened in a separate browser window (the content is a pdf document, which is why it is handled ouside of the SL app).

Now, to achieve this, I simply use

HtmlPage.Window.Navigate(new Uri(""), "_blank");

which works fine.

Now my client doesn't like the fact that every click opens up a new browser window. He would like to see the browser window reused every time an item is clicked. So I went out and tried implementing this:

Option 1 - Use the overload of the Navigate method, like so:

HtmlPage.Window.Navigate(new Uri(""), "foo");

I was assuming that the window would be reused when the same target parameter value (foo) would be used in subsequent calls.
This does not work. I get a new window every time.

Option 2 - Use the PopupWindow method on the HtmlPage

HtmlPage.PopupWindow(new Uri(""), "blah", new HtmlPopupWindowOptions());

This does not work. I get a new window every time.

Option 3 - Get a handle to the opened window and reuse that in subsequent calls

private HtmlWindow window;
private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    if (window == null)
        window = HtmlPage.Window.Navigate(new Uri(""), "blah");
    else
        window.Navigate(new Uri(""), "blah");

    if (window == null)
        MessageBox.Show("it's null");
}

This does not work. I tried the same for the PopupWindow() method and the window is null every time, so a new window is opened on every click. I have checked both the EnableHtmlAccess and the IsPopupWindowAllowed properties, and they return true, as they should.

Option 4 - Use Eval method to execute some custom javascript

private const string javascript = @"var popup = window.open('', 'blah') ; 
                                    if(popup.location != '' ){
                                        popup.location = '';
                                    }
                                    popup.focus();";

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval(javascript);
}

This does not work. I get a new window every time.

option 5 - Use CreateInstance to run some custom javascript on the page

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.CreateInstance("thisIsPlainHell");
}

and in my aspx I have

function thisIsPlainHell() {
    var popup = window.open('', 'blah');
}

This doesn't work. I get a new window every time.

Am I doing something wrong? I'm definitely no javascript expert, so I'm hoping it's something obvious I'm missing here.

Cheers, Phil

I want to use an external browser window to implement a preview functionality in a silverlight application. There is a list of items and whenever the user clicks one of these items, it's opened in a separate browser window (the content is a pdf document, which is why it is handled ouside of the SL app).

Now, to achieve this, I simply use

HtmlPage.Window.Navigate(new Uri("http://www.bing."), "_blank");

which works fine.

Now my client doesn't like the fact that every click opens up a new browser window. He would like to see the browser window reused every time an item is clicked. So I went out and tried implementing this:

Option 1 - Use the overload of the Navigate method, like so:

HtmlPage.Window.Navigate(new Uri("http://www.bing."), "foo");

I was assuming that the window would be reused when the same target parameter value (foo) would be used in subsequent calls.
This does not work. I get a new window every time.

Option 2 - Use the PopupWindow method on the HtmlPage

HtmlPage.PopupWindow(new Uri("http://www.bing."), "blah", new HtmlPopupWindowOptions());

This does not work. I get a new window every time.

Option 3 - Get a handle to the opened window and reuse that in subsequent calls

private HtmlWindow window;
private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    if (window == null)
        window = HtmlPage.Window.Navigate(new Uri("http://www.bing."), "blah");
    else
        window.Navigate(new Uri("http://www.bing."), "blah");

    if (window == null)
        MessageBox.Show("it's null");
}

This does not work. I tried the same for the PopupWindow() method and the window is null every time, so a new window is opened on every click. I have checked both the EnableHtmlAccess and the IsPopupWindowAllowed properties, and they return true, as they should.

Option 4 - Use Eval method to execute some custom javascript

private const string javascript = @"var popup = window.open('', 'blah') ; 
                                    if(popup.location != 'http://www.bing.' ){
                                        popup.location = 'http://www.bing.';
                                    }
                                    popup.focus();";

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval(javascript);
}

This does not work. I get a new window every time.

option 5 - Use CreateInstance to run some custom javascript on the page

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.CreateInstance("thisIsPlainHell");
}

and in my aspx I have

function thisIsPlainHell() {
    var popup = window.open('http://www.bing.', 'blah');
}

This doesn't work. I get a new window every time.

Am I doing something wrong? I'm definitely no javascript expert, so I'm hoping it's something obvious I'm missing here.

Cheers, Phil

Share Improve this question edited Mar 24, 2010 at 10:14 Phil asked Mar 24, 2010 at 9:19 PhilPhil 4074 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

This is a security feature of the browser. The re-use of named windows is limited to the domain of the document current being displayed by that window.

Your page hosting the Silverlight control will be running from "http://www.yoursite." hence it can only re-use window named "foo" if that window is also currently showing content from "http://www.yoursite.". Since your have loaded "http://www.bing." into that window it is no longer visible to code running in the context "http://www.yoursite.". Hence when you open a window using "Foo" the browser doesn't see a current window with that name and hence creates another one.

Are you sure it isn't a browser setting causing this? I use HtmlPage.Window.Navigate(new Uri(url)) and it always navigates the current tab that my silverlight control is sitting in.

Apparently the problem is that the window.name property is lost when navigating to bing.. When I navigate to one of my custom pages it seems to be working fine.

I want to use an external browser window to implement a preview functionality in a silverlight application. There is a list of items and whenever the user clicks one of these items, it's opened in a separate browser window (the content is a pdf document, which is why it is handled ouside of the SL app).

Now, to achieve this, I simply use

HtmlPage.Window.Navigate(new Uri(""), "_blank");

which works fine.

Now my client doesn't like the fact that every click opens up a new browser window. He would like to see the browser window reused every time an item is clicked. So I went out and tried implementing this:

Option 1 - Use the overload of the Navigate method, like so:

HtmlPage.Window.Navigate(new Uri(""), "foo");

I was assuming that the window would be reused when the same target parameter value (foo) would be used in subsequent calls.
This does not work. I get a new window every time.

Option 2 - Use the PopupWindow method on the HtmlPage

HtmlPage.PopupWindow(new Uri(""), "blah", new HtmlPopupWindowOptions());

This does not work. I get a new window every time.

Option 3 - Get a handle to the opened window and reuse that in subsequent calls

private HtmlWindow window;
private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    if (window == null)
        window = HtmlPage.Window.Navigate(new Uri(""), "blah");
    else
        window.Navigate(new Uri(""), "blah");

    if (window == null)
        MessageBox.Show("it's null");
}

This does not work. I tried the same for the PopupWindow() method and the window is null every time, so a new window is opened on every click. I have checked both the EnableHtmlAccess and the IsPopupWindowAllowed properties, and they return true, as they should.

Option 4 - Use Eval method to execute some custom javascript

private const string javascript = @"var popup = window.open('', 'blah') ; 
                                    if(popup.location != '' ){
                                        popup.location = '';
                                    }
                                    popup.focus();";

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval(javascript);
}

This does not work. I get a new window every time.

option 5 - Use CreateInstance to run some custom javascript on the page

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.CreateInstance("thisIsPlainHell");
}

and in my aspx I have

function thisIsPlainHell() {
    var popup = window.open('', 'blah');
}

This doesn't work. I get a new window every time.

Am I doing something wrong? I'm definitely no javascript expert, so I'm hoping it's something obvious I'm missing here.

Cheers, Phil

I want to use an external browser window to implement a preview functionality in a silverlight application. There is a list of items and whenever the user clicks one of these items, it's opened in a separate browser window (the content is a pdf document, which is why it is handled ouside of the SL app).

Now, to achieve this, I simply use

HtmlPage.Window.Navigate(new Uri("http://www.bing."), "_blank");

which works fine.

Now my client doesn't like the fact that every click opens up a new browser window. He would like to see the browser window reused every time an item is clicked. So I went out and tried implementing this:

Option 1 - Use the overload of the Navigate method, like so:

HtmlPage.Window.Navigate(new Uri("http://www.bing."), "foo");

I was assuming that the window would be reused when the same target parameter value (foo) would be used in subsequent calls.
This does not work. I get a new window every time.

Option 2 - Use the PopupWindow method on the HtmlPage

HtmlPage.PopupWindow(new Uri("http://www.bing."), "blah", new HtmlPopupWindowOptions());

This does not work. I get a new window every time.

Option 3 - Get a handle to the opened window and reuse that in subsequent calls

private HtmlWindow window;
private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    if (window == null)
        window = HtmlPage.Window.Navigate(new Uri("http://www.bing."), "blah");
    else
        window.Navigate(new Uri("http://www.bing."), "blah");

    if (window == null)
        MessageBox.Show("it's null");
}

This does not work. I tried the same for the PopupWindow() method and the window is null every time, so a new window is opened on every click. I have checked both the EnableHtmlAccess and the IsPopupWindowAllowed properties, and they return true, as they should.

Option 4 - Use Eval method to execute some custom javascript

private const string javascript = @"var popup = window.open('', 'blah') ; 
                                    if(popup.location != 'http://www.bing.' ){
                                        popup.location = 'http://www.bing.';
                                    }
                                    popup.focus();";

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval(javascript);
}

This does not work. I get a new window every time.

option 5 - Use CreateInstance to run some custom javascript on the page

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.CreateInstance("thisIsPlainHell");
}

and in my aspx I have

function thisIsPlainHell() {
    var popup = window.open('http://www.bing.', 'blah');
}

This doesn't work. I get a new window every time.

Am I doing something wrong? I'm definitely no javascript expert, so I'm hoping it's something obvious I'm missing here.

Cheers, Phil

Share Improve this question edited Mar 24, 2010 at 10:14 Phil asked Mar 24, 2010 at 9:19 PhilPhil 4074 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

This is a security feature of the browser. The re-use of named windows is limited to the domain of the document current being displayed by that window.

Your page hosting the Silverlight control will be running from "http://www.yoursite." hence it can only re-use window named "foo" if that window is also currently showing content from "http://www.yoursite.". Since your have loaded "http://www.bing." into that window it is no longer visible to code running in the context "http://www.yoursite.". Hence when you open a window using "Foo" the browser doesn't see a current window with that name and hence creates another one.

Are you sure it isn't a browser setting causing this? I use HtmlPage.Window.Navigate(new Uri(url)) and it always navigates the current tab that my silverlight control is sitting in.

Apparently the problem is that the window.name property is lost when navigating to bing.. When I navigate to one of my custom pages it seems to be working fine.

本文标签: