broad search for an object known to exist

Discussion in 'Javascript' started by Andrew Poulos, Jul 22, 2008.

  1. 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-=
     
    Andrew Poulos, Jul 22, 2008
    #1
    1. Advertisements

  2. 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>
     
    Thomas 'PointedEars' Lahn, Jul 22, 2008
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse
    Replies:
    1
    Views:
    434
    =?Utf-8?B?RGF2aWQgQ29lLCBNQ1A=?=
    Feb 10, 2004
  2. Random

    Namespaces - broad question

    Random, Apr 8, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    690
    Kevin Spencer
    Apr 8, 2004
  3. broad-based question

    , Nov 3, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    660
  4. Sam Hwang
    Replies:
    0
    Views:
    403
    Sam Hwang
    Apr 13, 2005
  5. LT
    Replies:
    7
    Views:
    2,350
    Phlip
    Jul 25, 2004
  6. Donald Braman

    rather broad question on data handling

    Donald Braman, Feb 17, 2004, in forum: Python
    Replies:
    1
    Views:
    303
    Jeff Sandys
    Feb 18, 2004
  7. BigZero
    Replies:
    1
    Views:
    362
    GArlington
    Mar 7, 2008
  8. Broad Question

    , Feb 7, 2006, in forum: Javascript
    Replies:
    5
    Views:
    207
    Dr John Stockton
    Feb 8, 2006
Loading...