broad search for an object known to exist

A

Andrew Poulos

I'm doing some work with various different Learning Management Systems
(LMS) and they each expose an object called "API". The problem is I
can't know beforehand how the user may decide to configure the LMS.

The LMS might open my page in a new window, in a frame, in a frame in a
new window... From my page I'm doing this search:

var fGetAPI = function(){
var numOfTries, lwindow, theAPI, i, j;
numOfTries = 20; // an arbitrary number
lwindow = window;
// Start by looking for the API in the current window
theAPI = lwindow.API;
if (!theAPI) {
for (i = 0; i < numOfTries; i++) {
while (!theAPI && lwindow.parent && lwindow.parent != lwindow) {
lwindow = lwindow.parent;
theAPI = lwindow.API;
if (!theAPI && lwindow.frames.length) {
for (j = 0; j < lwindow.frames.length; j++) {
theAPI = lwindow.frames.API;
if (theAPI) break;
}
}
}
if (!theAPI && lwindow.opener && !lwindow.opener.closed) {
lwindow = lwindow.opener;
theAPI = lwindow.API;
if (!theAPI && lwindow.frames.length) {
for (j = 0; j < lwindow.frames.length; j++) {
theAPI = lwindow.frames.API;
if (theAPI) break;
}
}
}
if (theAPI) break;
}
// if the API could not been found alert
if (!theAPI) {
theAPI = null;
alert("Unable to find API.");
} else alert("FOUND"); // for testing
}
return theAPI;
};
var locAPI = fGetAPI();

When I look at my code I can't help feeling that there must be a better
way to look through all possible windows.

Andrew Poulos-=
 
T

Thomas 'PointedEars' Lahn

Andrew said:
I'm doing some work with various different Learning Management Systems
(LMS) and they each expose an object called "API". The problem is I
can't know beforehand how the user may decide to configure the LMS.

The LMS might open my page in a new window, in a frame, in a frame in a
new window... From my page I'm doing this search:

var fGetAPI = function(){
var numOfTries, lwindow, theAPI, i, j;
numOfTries = 20; // an arbitrary number
lwindow = window;
// Start by looking for the API in the current window
theAPI = lwindow.API;
if (!theAPI) {
for (i = 0; i < numOfTries; i++) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is nonsense. Either the parent frameset or opener window is loaded or
it is not. If they are not loaded, the frame in which your script runs is
likely not to be loaded either. Anyhow, repeating something a limited
number of times in a tight loop is never likely to cover the issue reliably.
[...]
When I look at my code I can't help feeling that there must be a better
way to look through all possible windows.

There is. You might want to add recursion to your frame search. It would
also appear to be prudent to put this part of the search in a general method
that can be passed a Window object reference. And you must guard against
running into a reference loop, i.e. you need to create a list (an array) of
objects traversed so far and do not recurse if the object is in that list.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top