find largest frame

Discussion in 'Javascript' started by Jack, Oct 19, 2007.

  1. Jack

    Jack Guest

    Hello,

    What would be the easiest way to find the largest displayed document
    within a frameset? Here are the steps I would like to take in
    designing the algorithm:

    1. Attempt to find a top level 'body' element of XHTMl document. If
    found, return element.
    2. No 'body' found, so attempt to find a 'frameset'.
    3. Found 'frameset', so now find largest frame within 'frameset'.
    4. Found largest frame within frameset, return 'body' element of
    frame.
    5. If largest frame contained no 'body' element repeast step 2,
    continue until 'body' element found.

    Problem:

    1. What may appear to be the largest frame within a frameset, may in
    fact be the smallest, if it contains nested framsets.

    2. Seems messy.
    Jack, Oct 19, 2007
    #1
    1. Advertising

  2. Jack

    pr Guest

    Jack wrote:
    > What would be the easiest way to find the largest displayed document
    > within a frameset? Here are the steps I would like to take in
    > designing the algorithm:
    >
    > 1. Attempt to find a top level 'body' element of XHTMl document. If
    > found, return element.
    > 2. No 'body' found, so attempt to find a 'frameset'.
    > 3. Found 'frameset', so now find largest frame within 'frameset'.
    > 4. Found largest frame within frameset, return 'body' element of
    > frame.
    > 5. If largest frame contained no 'body' element repeast step 2,
    > continue until 'body' element found.
    >


    You're confusing HTML elements like <frame> with browser objects like
    window. It might just about be possible to navigate frames using the DOM
    but nobody in their right mind would try it.

    > Problem:
    >
    > 1. What may appear to be the largest frame within a frameset, may in
    > fact be the smallest, if it contains nested framsets.
    >
    > 2. Seems messy.
    >


    Here is a list of ingredients you want:

    window.top
    window.frames: both window.frames.length and window.frames
    recursion

    Once you have an array of window objects, you can go about the business
    of comparing them, possibly using window.innerWidth/Height, but see a
    recent thread about getting window sizes.
    pr, Oct 20, 2007
    #2
    1. Advertising

  3. Jack

    Jack Guest

    On Oct 20, 3:27 pm, pr <> wrote:
    > Jack wrote:
    > > What would be the easiest way to find the largest displayed document
    > > within a frameset? Here are the steps I would like to take in
    > > designing the algorithm:

    >
    > > 1. Attempt to find a top level 'body' element of XHTMl document. If
    > > found, return element.
    > > 2. No 'body' found, so attempt to find a 'frameset'.
    > > 3. Found 'frameset', so now find largest frame within 'frameset'.
    > > 4. Found largest frame within frameset, return 'body' element of
    > > frame.
    > > 5. If largest frame contained no 'body' element repeast step 2,
    > > continue until 'body' element found.

    >
    > You're confusing HTML elements like <frame> with browser objects like
    > window. It might just about be possible to navigate frames using the DOM
    > but nobody in their right mind would try it.
    >
    > > Problem:

    >
    > > 1. What may appear to be the largest frame within a frameset, may in
    > > fact be the smallest, if it contains nested framsets.

    >
    > > 2. Seems messy.

    >
    > Here is a list of ingredients you want:
    >
    > window.top
    > window.frames: both window.frames.length and window.frames
    > recursion
    >
    > Once you have an array of window objects, you can go about the business
    > of comparing them, possibly using window.innerWidth/Height, but see a
    > recent thread about getting window sizes.


    Hello,

    You've given me a few methods and attributes which will allow me to
    compare values, which is useful. But how does this address my problem
    of:

    '1. What may appear to be the largest frame within a frameset, may in
    fact be the smallest, if it contains nested framsets'.

    Sure I could use the methods / attributes you have provided to
    recursively descend framesets, but then I can imagine a horrible mess
    of trying to detect when the frame I'm currently (recursively) in gets
    smaller than a previous top level frame.

    Jack.
    Jack, Oct 23, 2007
    #3
  4. Jack

    Jack Guest

    On Oct 23, 2:52 pm, Jack <> wrote:
    > On Oct 20, 3:27 pm, pr <> wrote:
    >
    >
    >
    > > Jack wrote:
    > > > What would be the easiest way to find the largest displayed document
    > > > within a frameset? Here are the steps I would like to take in
    > > > designing the algorithm:

    >
    > > > 1. Attempt to find a top level 'body' element of XHTMl document. If
    > > > found, return element.
    > > > 2. No 'body' found, so attempt to find a 'frameset'.
    > > > 3. Found 'frameset', so now find largest frame within 'frameset'.
    > > > 4. Found largest frame within frameset, return 'body' element of
    > > > frame.
    > > > 5. If largest frame contained no 'body' element repeast step 2,
    > > > continue until 'body' element found.

    >
    > > You're confusing HTML elements like <frame> with browser objects like
    > > window. It might just about be possible to navigate frames using the DOM
    > > but nobody in their right mind would try it.

    >
    > > > Problem:

    >
    > > > 1. What may appear to be the largest frame within a frameset, may in
    > > > fact be the smallest, if it contains nested framsets.

    >
    > > > 2. Seems messy.

    >
    > > Here is a list of ingredients you want:

    >
    > > window.top
    > > window.frames: both window.frames.length and window.frames
    > > recursion

    >
    > > Once you have an array of window objects, you can go about the business
    > > of comparing them, possibly using window.innerWidth/Height, but see a
    > > recent thread about getting window sizes.

    >
    > Hello,
    >
    > You've given me a few methods and attributes which will allow me to
    > compare values, which is useful. But how does this address my problem
    > of:
    >
    > '1. What may appear to be the largest frame within a frameset, may in
    > fact be the smallest, if it contains nested framsets'.
    >
    > Sure I could use the methods / attributes you have provided to
    > recursively descend framesets, but then I can imagine a horrible mess
    > of trying to detect when the frame I'm currently (recursively) in gets
    > smaller than a previous top level frame.
    >
    > Jack.


    In and attempt to clarify the problem, imagine:

    A web page with a frameset called fsA. fsA is made up up two frames,
    called f1 and f2. Using JavaScript I detect that frame f1 is wider
    than frame f2. I descend into frame f1 and find that it contains a
    frameset fsB containing two frames f3 and f4. Here it is possible
    that frames f3 and f4 are both less wide than unvisited frame f2 of
    frameset fsA. In order to know this I would have to have stored the
    width of frame f2, even though I was not descending into it. In other
    words I would have to remember the width of all frames as I
    recursively descend into framesets. The point at which I am still
    recursively descending AND the frame is less wide than a previously
    unvisited frame, I should bail out and commence descending into
    previously unvisited frame to see if I can't find a wider frame.

    This is extremely messy and would be difficult to implement.
    Jack, Oct 23, 2007
    #4
  5. Jack

    pr Guest

    Jack wrote:
    > On Oct 20, 3:27 pm, pr <> wrote:
    >> Jack wrote:
    >>> What would be the easiest way to find the largest displayed document
    >>> within a frameset?
    >>> [...]

    >> Here is a list of ingredients you want:
    >>
    >> window.top
    >> window.frames: both window.frames.length and window.frames
    >> recursion
    >>
    >> Once you have an array of window objects, you can go about the business
    >> of comparing them, possibly using window.innerWidth/Height, but see a
    >> recent thread about getting window sizes.

    >
    > Hello,
    >
    > You've given me a few methods and attributes which will allow me to
    > compare values, which is useful. But how does this address my problem
    > of:
    >
    > '1. What may appear to be the largest frame within a frameset, may in
    > fact be the smallest, if it contains nested framsets'.
    >
    > Sure I could use the methods / attributes you have provided to
    > recursively descend framesets, but then I can imagine a horrible mess
    > of trying to detect when the frame I'm currently (recursively) in gets
    > smaller than a previous top level frame.
    >


    I think you're being pessimistic. The simple code below will show a
    pretty-printed list of current frame names and sizes (so long as all the
    content comes from the same domain). Maybe that'll make things clearer.

    var tabs = "\n\t\t\t\t\t\t\t";

    function getFrames(w, level) {
    var f = w.frames, l = w.frames.length;
    var indent = tabs.substring(0, level), i, fm, s= "";

    for (i = 0; i < l; i++) {
    fm = f;
    s += indent + " frame name " + fm.name;
    s += getFrames(fm, level +1);
    }

    if (!l) {
    s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
    (" + (w.innerWidth * w.innerHeight) + "px)";
    }

    return s;
    }

    alert(getFrames(window.top, 1));
    pr, Oct 23, 2007
    #5
  6. Jack

    David Mark Guest

    On Oct 23, 12:25 pm, pr <> wrote:
    > Jack wrote:
    > > On Oct 20, 3:27 pm, pr <> wrote:
    > >> Jack wrote:
    > >>> What would be the easiest way to find the largest displayed document
    > >>> within a frameset?
    > >>> [...]
    > >> Here is a list of ingredients you want:

    >
    > >> window.top
    > >> window.frames: both window.frames.length and window.frames
    > >> recursion

    >
    > >> Once you have an array of window objects, you can go about the business
    > >> of comparing them, possibly using window.innerWidth/Height, but see a
    > >> recent thread about getting window sizes.

    >
    > > Hello,

    >
    > > You've given me a few methods and attributes which will allow me to
    > > compare values, which is useful. But how does this address my problem
    > > of:

    >
    > > '1. What may appear to be the largest frame within a frameset, may in
    > > fact be the smallest, if it contains nested framsets'.

    >
    > > Sure I could use the methods / attributes you have provided to
    > > recursively descend framesets, but then I can imagine a horrible mess
    > > of trying to detect when the frame I'm currently (recursively) in gets
    > > smaller than a previous top level frame.

    >
    > I think you're being pessimistic. The simple code below will show a
    > pretty-printed list of current frame names and sizes (so long as all the
    > content comes from the same domain). Maybe that'll make things clearer.
    >
    > var tabs = "\n\t\t\t\t\t\t\t";
    >
    > function getFrames(w, level) {
    > var f = w.frames, l = w.frames.length;
    > var indent = tabs.substring(0, level), i, fm, s= "";
    >
    > for (i = 0; i < l; i++) {
    > fm = f;
    > s += indent + " frame name " + fm.name;
    > s += getFrames(fm, level +1);
    > }
    >
    > if (!l) {
    > s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
    > (" + (w.innerWidth * w.innerHeight) + "px)";
    > }
    >
    > return s;
    > }
    >
    > alert(getFrames(window.top, 1));- Hide quoted text -
    >


    Of course, that won't do anything useful in IE, which doesn't support
    innerWidth/Height properties.

    This is untested with a frameset, though I did simulate one with a
    couple of nested IFrames.

    var windowArea = (function() {
    if (typeof(this.innerHeight) == 'number') {
    return function(w) { return w.innerHeight * w.innerWidth; };
    }
    return function(w) {
    var doc = w.document;
    var prop = (doc.documentElement && doc.compatMode &&
    doc.compatMode.indexOf('CSS') != -1)?'documentElement':'body';
    return doc[prop].clientHeight * doc[prop].clientWidth;
    };
    })();

    function maxFrame(w) {
    var obj, frm, max = 0, f = w.frames, l = f.length;

    if (!l) {
    return {win:w, size:windowArea(w)};
    }
    while (l) {
    obj = maxFrame(f[--l]);
    if (obj.size > max) {
    max = obj.size;
    frm = obj.win;
    }
    }
    return {win:frm, size:max};
    }

    function getBody(w) {
    var doc = w.document;
    return doc.body || (doc.getElementsByTagName &&
    doc.getElementsByTagName('body')[0]);
    }

    function maxWindow(w) {
    var body = getBody(w);
    return (body)?w:maxFrame(w).win;
    }

    function maxBody(w) {
    return getBody(maxWindow(w));
    }
    David Mark, Oct 23, 2007
    #6
  7. Jack

    Jack Guest

    On Oct 24, 10:28 am, Jack <> wrote:
    > On Oct 23, 8:10 pm, David Mark <> wrote:
    >
    >
    >
    > > On Oct 23, 12:25 pm, pr <> wrote:

    >
    > > > Jack wrote:
    > > > > On Oct 20, 3:27 pm, pr <> wrote:
    > > > >> Jack wrote:
    > > > >>> What would be the easiest way to find the largest displayed document
    > > > >>> within a frameset?
    > > > >>> [...]
    > > > >> Here is a list of ingredients you want:

    >
    > > > >> window.top
    > > > >> window.frames: both window.frames.length and window.frames
    > > > >> recursion

    >
    > > > >> Once you have an array of window objects, you can go about the business
    > > > >> of comparing them, possibly using window.innerWidth/Height, but see a
    > > > >> recent thread about getting window sizes.

    >
    > > > > Hello,

    >
    > > > > You've given me a few methods and attributes which will allow me to
    > > > > compare values, which is useful. But how does this address my problem
    > > > > of:

    >
    > > > > '1. What may appear to be the largestframewithin a frameset, may in
    > > > > fact be the smallest, if it contains nested framsets'.

    >
    > > > > Sure I could use the methods / attributes you have provided to
    > > > > recursively descend framesets, but then I can imagine a horrible mess
    > > > > of trying to detect when theframeI'm currently (recursively) in gets
    > > > > smaller than a previous top levelframe.

    >
    > > > I think you're being pessimistic. The simple code below will show a
    > > > pretty-printed list of currentframenames and sizes (so long as all the
    > > > content comes from the same domain). Maybe that'll make things clearer.

    >
    > > > var tabs = "\n\t\t\t\t\t\t\t";

    >
    > > > function getFrames(w, level) {
    > > > var f = w.frames, l = w.frames.length;
    > > > var indent = tabs.substring(0, level), i, fm, s= "";

    >
    > > > for (i = 0; i < l; i++) {
    > > > fm = f;
    > > > s += indent + "framename " + fm.name;
    > > > s += getFrames(fm, level +1);
    > > > }

    >
    > > > if (!l) {
    > > > s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
    > > > (" + (w.innerWidth * w.innerHeight) + "px)";
    > > > }

    >
    > > > return s;
    > > > }

    >
    > > > alert(getFrames(window.top, 1));- Hide quoted text -

    >
    > > Of course, that won't do anything useful in IE, which doesn't support
    > > innerWidth/Height properties.

    >
    > > This is untested with a frameset, though I did simulate one with a
    > > couple of nested IFrames.

    >
    > > var windowArea = (function() {
    > > if (typeof(this.innerHeight) == 'number') {
    > > return function(w) { return w.innerHeight * w.innerWidth; };
    > > }
    > > return function(w) {
    > > var doc = w.document;
    > > var prop = (doc.documentElement && doc.compatMode &&
    > > doc.compatMode.indexOf('CSS') != -1)?'documentElement':'body';
    > > return doc[prop].clientHeight * doc[prop].clientWidth;
    > > };
    > > })();

    >
    > > function maxFrame(w) {
    > > var obj, frm, max = 0, f = w.frames, l = f.length;

    >
    > > if (!l) {
    > > return {win:w, size:windowArea(w)};
    > > }
    > > while (l) {
    > > obj = maxFrame(f[--l]);
    > > if (obj.size > max) {
    > > max = obj.size;
    > > frm = obj.win;
    > > }
    > > }
    > > return {win:frm, size:max};
    > > }

    >
    > > function getBody(w) {
    > > var doc = w.document;
    > > return doc.body || (doc.getElementsByTagName &&
    > > doc.getElementsByTagName('body')[0]);
    > > }

    >
    > > function maxWindow(w) {
    > > var body = getBody(w);
    > > return (body)?w:maxFrame(w).win;
    > > }

    >
    > > function maxBody(w) {
    > > return getBody(maxWindow(w));
    > > }

    >
    > On Oct 23, 8:10 pm, David Mark <> wrote:
    >
    >
    >
    > > On Oct 23, 12:25 pm, pr <> wrote:

    >
    > > > Jack wrote:
    > > > > On Oct 20, 3:27 pm, pr <> wrote:
    > > > >> Jack wrote:
    > > > >>> What would be the easiest way to find the largest displayed document
    > > > >>> within a frameset?
    > > > >>> [...]
    > > > >> Here is a list of ingredients you want:

    >
    > > > >> window.top
    > > > >> window.frames: both window.frames.length and window.frames
    > > > >> recursion

    >
    > > > >> Once you have an array of window objects, you can go about the business
    > > > >> of comparing them, possibly using window.innerWidth/Height, but see a
    > > > >> recent thread about getting window sizes.

    >
    > > > > Hello,

    >
    > > > > You've given me a few methods and attributes which will allow me to
    > > > > compare values, which is useful. But how does this address my problem
    > > > > of:

    >
    > > > > '1. What may appear to be the largestframewithin a frameset, may in
    > > > > fact be the smallest, if it contains nested framsets'.

    >
    > > > > Sure I could use the methods / attributes you have provided to
    > > > > recursively descend framesets, but then I can imagine a horrible mess
    > > > > of trying to detect when theframeI'm currently (recursively) in gets
    > > > > smaller than a previous top levelframe.

    >
    > > > I think you're being pessimistic. The simple code below will show a
    > > > pretty-printed list of currentframenames and sizes (so long as all the
    > > > content comes from the same domain). Maybe that'll make things clearer.

    >
    > > > var tabs = "\n\t\t\t\t\t\t\t";

    >
    > > > function getFrames(w, level) {
    > > > var f = w.frames, l = w.frames.length;
    > > > var indent = tabs.substring(0, level), i, fm, s= "";

    >
    > > > for (i = 0; i < l; i++) {
    > > > fm = f;
    > > > s += indent + "framename " + fm.name;
    > > > s += getFrames(fm, level +1);
    > > > }

    >
    > > > if (!l) {
    > > > s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
    > > > (" + (w.innerWidth * w.innerHeight) + "px)";
    > > > }

    >
    > > > return s;
    > > > }

    >
    > > > alert(getFrames(window.top, 1));- Hide quoted text -

    >
    > > Of course, that won't do anything useful in IE, which doesn't support
    > > innerWidth/Height properties.

    >
    > > This is untested with a frameset, though I did simulate one with a
    > > couple of nested IFrames.

    >
    > > var windowArea = (function() {
    > > if (typeof(this.innerHeight) == 'number') {
    > > return function(w) { return w.innerHeight * w.innerWidth; };
    > > }
    > > return function(w) {
    > > var doc = w.document;
    > > var prop = (doc.documentElement && doc.compatMode &&
    > > doc.compatMode.indexOf('CSS') != -1)?'documentElement':'body';
    > > return doc[prop].clientHeight * doc[prop].clientWidth;
    > > };
    > > })();

    >
    > > function maxFrame(w) {
    > > var obj, frm, max = 0, f = w.frames, l = f.length;

    >
    > > if (!l) {
    > > return {win:w, size:windowArea(w)};
    > > }
    > > while (l) {
    > > obj = maxFrame(f[--l]);
    > > if (obj.size > max) {
    > > max = obj.size;
    > > frm = obj.win;
    > > }
    > > }
    > > return {win:frm, size:max};
    > > }

    >
    > > function getBody(w) {
    > > var doc = w.document;
    > > return doc.body || (doc.getElementsByTagName &&
    > > doc.getElementsByTagName('body')[0]);
    > > }

    >
    > > function maxWindow(w) {
    > > var body = getBody(w);
    > > return (body)?w:maxFrame(w).win;
    > > }

    >
    > > function maxBody(w) {
    > > return getBody(maxWindow(w));
    > > }

    >
    > Well thank you both, extremely nice examples and very helpful. I am
    > in fact primarily concerned with Firefox, although may at a later date
    > want to incorporate IE. I have the idea now that it would be better
    > to first build up a data structure, perhaps an array, of objects that
    > model the data I require, for example I could have an array of myFrame
    > objects, built up using the methods posted here:
    >
    > var myFrame =
    > {
    > friendlyName : "",
    > reference : null,
    > width : -1,Q
    > height : -1;
    >
    > }
    >
    > Once I had this data structure I could search for the widestframe.
    > Perhaps even caching the result for quicker reference in the future.
    > IN fact I could do many useful things with the data (sort etc). Does
    > this sound sensible?
    >
    > Thank you both. I will try and post my results and solution here.


    I have written a small script, pretty much just using the function
    getFrames, posted by pr. However, in order for this script to work I
    have to place it in the inner-most frame. Presumably this is because
    it is at this point all frames have been loaded? Is there a way to
    run the script from the top level page (containing the top most
    framset)?

    Many thanks,

    Jack
    Jack, Oct 26, 2007
    #7
  8. Jack

    pr Guest

    Jack wrote:
    > [big snip]


    Please only quote the part you're replying to.

    >
    > I have written a small script, pretty much just using the function
    > getFrames, posted by pr. However, in order for this script to work I
    > have to place it in the inner-most frame. Presumably this is because
    > it is at this point all frames have been loaded? Is there a way to
    > run the script from the top level page (containing the top most
    > framset)?
    >


    If you're running the script close to when the frameset(s) load(s), then
    you might have an issue with partially-downloaded content and it's
    probably only an accident that it appears to work in the innermost page.
    If you describe the symptoms and your frameset structure, I can look
    into it.
    pr, Oct 26, 2007
    #8
    1. Advertising

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. PHP2
    Replies:
    2
    Views:
    408
    ¾ç¼¼ÈÆ
    Apr 29, 2004
  2. Code4u
    Replies:
    4
    Views:
    2,643
    Stephen Howe
    Jul 13, 2005
  3. RashmiRaju
    Replies:
    0
    Views:
    339
    RashmiRaju
    Feb 29, 2008
  4. Script to find largest files

    , Nov 1, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    118
    Peter J. Holzer
    Nov 3, 2007
  5. Sudrien
    Replies:
    2
    Views:
    109
    Sudrien
    Aug 4, 2007
Loading...

Share This Page