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-=
(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-=