Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > broad search for an object known to exist

Reply
Thread Tools

broad search for an object known to exist

 
 
Andrew Poulos
Guest
Posts: n/a
 
      07-22-2008
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[i].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[i].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-=
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-22-2008
Andrew Poulos wrote:
> 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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does any widely-known program to extract template parameters requirements from source code already exist? Pavel Shved C++ 11 11-12-2007 09:22 AM
Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control) LT C++ 7 07-25-2004 07:08 PM
Can the Favourites Sidebar (IE, Opera, etc.) be addressed selectively (exist or non-exist)? Markus Mohr Javascript 7 11-28-2003 12:20 AM
In Schema, how to say "If one element exist, another element must exist"? Y.S. XML 3 09-17-2003 02:51 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments