Frame handling

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

    #1

    Frame handling

    I'm looking for tutorials or articles considering HTML Frames and how to
    handle them with Javascript.

    E.g. samples how Frames can exchange information, can a Frame instruct
    other Frame to update it's content etc.

    This kind of information is welcome.

    Cheers!



  • Richard Cornford

    #2
    Re: Frame handling

    Frame wrote:[color=blue]
    > I'm looking for tutorials or articles considering HTML
    > Frames and how to handle them with Javascript.
    >
    > E.g. samples how Frames can exchange information, can a
    > Frame instruct other Frame to update it's content etc.
    >
    > This kind of information is welcome.[/color]

    First off, be very certain that you need to be using frames at all. They
    introduce a lot of issues that do not exist in single web pages, some of
    which are significant to scripting such structures.

    For single web page the scripting environment has a single global
    object, and that global object is the 'window' object. An object that
    can be referred to by the identifiers - window - and - self - (and,
    because there is no frameset hierarchy, by the identifiers - parent -
    and - top -, and from code executing in the global context, or from
    within function not being executed as methods of objects, with the -
    this - keyword).

    In a frameset each page (including any pages defining a frameset) has
    its own global object, which is also a 'window' object (though it would
    be better referred to as a 'frame' object in that context). Thus code
    executing within any page in the frameset can refer to its own
    global/window/frame object using the unqualified identifiers - window -
    and - self -. However, these global/window/frame objects form a
    tree-like hierarchy in a frameset. The unqualified identifier - top -
    refers to the top-most (or root) global/window/frame object in that
    hierarchy from any frame. And the identifier - parent - refers to the
    global/window/frame object that directly contains the
    global/window/frame object from which the reference is made (The one
    which has loaded the html page that includes the FRAME element that
    loaded the page using the - parent - reference).

    Global variables and function definitions become named properties of the
    global object that contains the document in which they are defined (or
    which imports the script files in which they are defined). This means
    that if a (top-most) frameset page defines a function called -
    doSomething - then code imported into one of the pages within that
    frameset can call that function as:-

    top.toSomething (x, y);

    Similarly, if the top-most frameset page has a global variable call -
    something - then a page within the frameset could, for example, assign a
    value to that variable as:-

    top.something = 42;

    A page that is a direct child of the top-most frameset page could use:-

    parent.toSometh ing(x, y);

    - and -

    parent.somethin g = 42;

    and if the top-most frameset page defines a frame that itself loaded a
    frameset a page within that contained frameset could use:-

    parent.parent.t oSomething(x, y);

    - and -

    parent.parent.s omething = 42;

    Referencing the global/window/frame objects from parent to child is done
    through the - frames - collection, which has an indexed member for each
    frame (including IFRAME) directly contained within a frameset page. The
    members of the - frames - collection can also be referenced by name,
    that is, the - frames - collection has a named member, the name of which
    corresponds with the NAME attribute of the defined FRAME element. More
    recent browsers also support named references within the -
    frames -collection using names that correspond with the ID attribute of
    FRAME elements, but to maximise support it is probably advisable to give
    FRAME elements both NAME and ID attributes that have identical values
    (assuming the pertinent DTD allows that, as they probably will as strict
    DTDs don't accommodate FRAME elements anyway).

    So a frameset page that defines a FRAME element as:-

    <frame src="http://exampele.com/page.html" name="main" id="main">

    - can reference the global/window/frame object that contains -
    page.html - using:-

    frames['main']

    and if that frameset specified a second frame:-

    <frame src="http://exampele.com/page2.html" name="menu" id=" menu ">

    - then code within that second frame can refer to its sibling (main)
    as:-

    parent.frames['main']

    It is important to note that the - frames - collection only contains
    references to the global/window/frame objects that are direct children
    of the global/window/frame object that possesses the particular -
    frames - collection. Frames that are children of those contained frames
    need to be referenced through their parent's - frames - collection. So,
    in the above example, if - main - was itself a frameset that defined
    FRAME elements with the names/IDs "mainLeft" and "mainRight" then code
    in the top-most frameset page would refer to one of those two frames
    as:-

    frames['main'].frames['mainLeft']

    (Though probably a bit more cautiously than that as there would be no
    guarantee that main had loaded its HTML and so its - frames - collection
    may be empty at the time when the property accessor was evaluated.)

    Given that the global/window/frame objects that correspond with all of
    the windows/frames defined in a frameset (or nested framesets) form a
    tree-like hierarchy they can all be referenced from any locution in that
    structure using appropriate combinations of - parent -, - top -, and -
    frames - in the property accessors.

    Richard.


    Comment

    • Bert

      #3
      Re: Frame handling


      "Frame" <frame@mail.n o> schreef in bericht
      news:38eg99F5m0 tl8U1@individua l.net...[color=blue]
      > I'm looking for tutorials or articles considering HTML Frames and how to
      > handle them with Javascript.
      >
      > E.g. samples how Frames can exchange information, can a Frame instruct
      > other Frame to update it's content etc.
      >
      > This kind of information is welcome.
      >
      > Cheers!
      >
      >
      >[/color]

      Hi, I made a function for this that loops over all frames in the current
      window and tries to execute specific code on these frames.
      So when function is called it tries to execute it on all frames it can find,
      if function doesn't exist, it will do nothing.
      (it works on firefox and iexplore). Well I use it quite often because it is
      just one line of code and everything goes cross frame. There are some
      drawbacks, one of them is you must yourself manage the executable codes in
      the frames to avoid unwanted execution.


      you could for example call: firecode('alert (\"I am a frame\");');
      And every frame shows an alert.

      or firecode('chang ecolor();');
      and every frame that has changecolor() in it will execute it. What
      changecolor is is up to you :)
      Just make a function changecolor that changes the background of some frame,
      and call above code.




      here is the function:



      function firecode(code)
      {
      framestruct(win dow, 1, code);
      }

      function framestruct(el, start, func)
      {
      var m = 20;


      if(!start)
      {
      if((navigator && navigator.userA gent && navigator.userA gent.indexOf &&
      navigator.userA gent.indexOf('G ecko') != -1) ||
      (navigator.user Agent.indexOf(" Netscape")!= -1))
      {
      try
      {
      el.eval("try{" + func + "}catch(e){ }");
      }
      catch(e)
      {}
      }
      else
      {
      try{el.execScri pt("try{" + func + "}catch(e){}"); }catch(e){};
      }
      }
      else
      {

      for(;m>=0&&el.p arent;m--)
      {
      el = el.parent;
      }
      }


      var f = el.frames;
      var i;

      for(i=0; i<f.length; i++)
      {
      framestruct(f[i], 0, func);
      }

      }



      greetz Bert


      Comment

      • Csaba2000

        #4
        Re: Frame handling

        In the following case, the browsers (IE6 and Firefox 1.0) do not
        complain and show (via the alert) that an IMG element was
        inserted as the first child of the top's body element. And yet it is
        not displayed. Is this reasonable or not?

        Csaba Gabor from Vienna


        Main.htm:
        <html><head><ti tle>Frame testing</title></head>
        <frameset><fram e src="TestFrame. htm"></frameset></html>

        TestFrame.htm
        <html><head><ti tle>Image inserter</title>
        <script type='text/javascript'>
        function InsertImg() {
        var myimg=top.docum ent.createEleme nt('img');
        myimg.src="Test Img.jpg";
        var firstKid=top.do cument.body.chi ldNodes[0];
        top.document.bo dy.insertBefore (myimg,firstKid );
        alert(top.docum ent.body.childN odes[0].tagName);
        }
        </script></head>
        <body onLoad="InsertI mg()">
        Frame loaded
        </body></html>


        Comment

        • Marc Poulin

          #5
          Re: Frame handling

          Frame wrote:
          [color=blue]
          > I'm looking for tutorials or articles considering HTML Frames and how to
          > handle them with Javascript.
          >
          > E.g. samples how Frames can exchange information, can a Frame instruct
          > other Frame to update it's content etc.
          >
          > This kind of information is welcome.
          >
          > Cheers![/color]

          "JavaScript Application Cookbook" has lots of frame examples.
          You can read a sample chapter here:
          http://www.oreilly.com/catalog/jscook/chapter/ch01.html

          Comment

          • Richard Cornford

            #6
            Re: Frame handling

            Csaba2000 wrote:[color=blue]
            > In the following case, the browsers (IE6 and Firefox 1.0)
            > do not complain and show (via the alert) that an IMG element
            > was inserted as the first child of the top's body element.
            > And yet it is not displayed. Is this reasonable or not?[/color]
            <snip>[color=blue]
            > Main.htm:
            > <html><head><ti tle>Frame testing</title></head>
            > <frameset><fram e src="TestFrame. htm"></frameset></html>[/color]
            <snip>

            It depends on what you consider reasonable. You are adding an IMG as a
            child of a FRAMESET and a sibling of a FRAME. The HTML DTDs don't allow
            for that and a browser would be within its right to throw some sort of
            malformed DOM exception at the attempt. Not throwing such an exception
            is demonstrating tolerance, and being tolerant is probably a reasonable
            response to the extremely low level of technical rigour in general
            Internet authoring.

            That the browsers do not show an IMG element inserted in such an
            unexpected context is not particularly surprising either (though there
            was a single Mozilla release that would display an absolutely positioned
            DIV when inserted into a FRAMESET. Obviously that was recognised as a
            bug and fixed in subsequent versions).

            Richard.


            Comment

            Working...