broad search for an object known to exist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poulos

    broad search for an object known to exist

    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-=
  • Thomas 'PointedEars' Lahn

    #2
    Re: broad search for an object known to exist

    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>

    Comment

    Working...