Link to home
Start Free TrialLog in
Avatar of huangs3
huangs3Flag for Canada

asked on

SilverLight 4 and Javascript: how to search for an existing IE window within a set of names?

Hi experts:
    I am trying to make a Silverlight application that does the following in sequence (or something equivilent at the end):
1. Check whether there is an IE window named "xyz_1", "xyz_2", or "xyz_3", etc
2. If there is "xyz_n" window running, open "http://www.google.com" with any one of them;
3. If no "xyz_n" window is running, then popup an message.
    I find the System.windows.Browser.HtmlPage.Window.Navigate function is related, but still not able to implement what I expect. For example, if I do:
*******************************************************************
String url = "http://www.google.com";
HtmlWindow win1 = HtmlPage.Window.Navigate(new Uri(url), "xyz_1");
HtmlWindow win2 = HtmlPage.Window.Navigate(new Uri(url), "xyz_2");
HtmlWindow win3 = HtmlPage.Window.Navigate(new Uri(url), "xyz_3");
//...
*******************************************************************
1. many IE windows will popup unnecessarily
2. the programming cannot decide whether there is any xyz_n window running
    I also thought about using Html bridge to call the following BLOCKED SCRIPT
*************************************************************
void function openLink(Url) {
    windowFound = false;
    // search for an existing window to open the URL
    for (var seqNumber = 1; seqNumber < 100; seqNumber++) {
        testwindow = window.open("", "xyz_" + seqNumber);
        if (testwindow == null) {continue;}
        try {
            var dummy = testwindow.location.pathname;
            testwindow.close();         }
        catch (err) {
            windowFound = true;
            testwindow.location = Url;
            break;
        }
    }
    if (!windowFound) {alert("Please open xyz_n first");}
}
****************************************************************
However, it is still not a good solution because:
1. this javascript put the normal business logic into the catch-clause. This is not a good programming style
2. brute-force algorithm is used to search for existing "xyz_n" window, so there maybe performance issue.
3. it is unknown how large n can be, so there is always a chance to miss in the search.

My deployment environment is Silverlight 4, .Net framework 4, IE 7, Windows XP SP3.

Please let me know if there is any suggestion to improve this. Thank you!
Avatar of leakim971
leakim971
Flag of Guadeloupe image

With Silverlight, you may store each window reference (win1,win2, win3,...) created in an array and create a new function to close window(s).
You call this function from javascript, check the Call Silverlight Methods From JavaScript section HERE  for example

Avatar of huangs3

ASKER

Hi leakim971:

    Yes I can close a window by win1.Invoke("close"). However, after getting win1, win2, win3, I haven't found the way to decide which windows I shall close, and which one is the one that need to be left open....
    Any idea? Thank you.
You want to let the last one opened ? What's your goal? Do you store the name of the windows or the ref ? It's better to store the refereences in the array.
Avatar of huangs3

ASKER

Hi leakim971:

    The situation is: the window I want to open a URL from is not initially opened by the application I am writting. I am making a program to interact with the Oracle WAM system.

    The IE window name of Oracle WAM system is like "xyz_n", where n is a sequence number.
    I want to write a program to test from "xyz_1" to "xyz_100", such that it can find the Oracle WAM window and change its URL.

   The Oracle WAM window is not opened by the application I am writting, so I just have no way to let it save the reference at the beginning....

    What shall I do?
as I know there's no way to get ref of current windows opened without using a loop.
a loop like the one you did or using the window object (bad solution too)

No way to modify a little bit the Oracle application?
Avatar of huangs3

ASKER

leakim971:
    Unfortunately I don't know anyway of modifying the Oracle application...
    Do you have any better mehtod than what I did to examine whether the window is opened is a new one or an existing window?
    Thank you.
>Do you have any better mehtod than what I did to examine whether the window is opened is a new one or an existing window?

Sorry, no.
Avatar of huangs3

ASKER

oh...ok. Seems the javascript language itself need to be improved...
Thank you leakim971. I will keep this question open for several more days.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
With JS, I don't think it's possible to translate : HtmlPage.Window.Navigate(new Uri(url), "xyz_"+seqNum.ToString());
Avatar of huangs3

ASKER

Can just do this in JS:

testwindow = window.open(url, "xyz_" + seqNum)
yes of couse
Avatar of huangs3

ASKER

leakim971 indicated that it is impossible to do to much to the window that is not opened by you because of security, and I think that's right. Points are assigned to leakim971.