Results 1 to 9 of 9

Thread: JavaScript (DOM): Code across frames?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Unhappy JavaScript (DOM): Code across frames?

    Hi,
    What I need is to access document objects in one frame from another, to call functions in such frame, to share one variable in all frames of document and so on. Somewhere (perhaps on MSDN ) I read this is disabled because of security, but I would like to allow my code, objects, functions, variables, ... to be 'inherited' by child (or same leveled) iframes.
    Is there any way how to do it? Or how is this problem being solved?

    Thanks,
    John

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I never heard of that being disabled, and code like this has always worked for me:

    Code:
    <frame name="head" id="head" src="bla.html" />
    <frame name="body" id="body" src="flop.html" />
    
    // flop.html:
    <script type="text/javascript">
    function xyz()
    {
      alert(window.parent.frames['head'].document.getElementById('searchbox').value);
    }
    </script>
    
    // bla.html
    <script type="text/javascript">
    function onButtonClick()
    {
      window.parent.frames['body'].xyz();
    }
    </script>
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Well the problem I think is will work only in one level of frames. In the next one, you would use window.parent.parent.document to acces the main document, then parent.parent.parent and so on.
    Although I found this method quite strange, the problem could be solved ... until ... until I don't know what the calling frame is level of.
    Even this can be done, using window.parent.parent and many 'preventive' .parents added to it.

    Actually, my situation is (simplified) like this:
    Code:
    Root frame
     |
     +-- Child1A
     |     |
     |     '---- Child2A
     |
     +-- Child1B
           |
           '---- Child2B
                   |
                   '---- Child3
    and the task is to acces Child2A elements from Child3, without knowledge that Child3 is actually '3rd child' and Child2A being 'child of 1A'.

    So looking for something like wholebrowserwindow.allnestedframes('Child2A') or at least to simply get true parent, resp. browser window.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    When do you have a frameset page in a frameset page?

    For nested frameset tags in one page there's only one parent object.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Well actually I don't have any frameset page , I have standard page, with about three iframes in it, in which there are some other nested iframes and so on...

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But actually I see no reason why it shouldn't work for more than one level!

    Code:
    base.html
    
    <iframe id="ifr1" src="ifr1.html">No iframes</iframe>
    <iframe id="ifr2" src="ifr2.html">No iframes</iframe>
    Code:
    ifr1.html
    <iframe id="ifr11" src="ifr11.html">No iframes</iframe>
    Code:
    ifr2.html
    <script type="text/javascript">
    function hideSomething()
    {
      window.parent.document.getElementById(
        'ifr1').document.getElementById('ifr11').
        document.getElementById('hideable').
        style.visibility = "hidden";
    }
    </script>
    Code:
    ifr11.html
    <script type="text/javascript">
    function callWeird()
    {
      window.parent.parent.document.
        getElementById('ifr2').hideSomething();
    }
    </script>
    To lazy to try it, but I think it should work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But of course that would require you to know the exact structure...

    Hmmm...
    Code:
    function getAllIFrames()
    {
        var ret = new Array();
        var par = window;
        while(par.parent) par = par.parent;
        var allframes = par.document.getElementsByTagName('iframe');
        for(var i = 0; i < allframes.length; ++i) {
            ret[allframes[i].id] = allframes[i];
            var locals = allframes[i].document.getElementsByTagName('iframe');
            for(var j = 0; j < locals.length; ++j) {
                allframes[allframes.length] = locals[j];
            }
        }
        return ret;
    }
    Make sure all the iframes have id attributes and they are ALL unique all across the pages.

    Now if that works I really have to pat myself on the back
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Wow, cool ! I was thinking about enumerating all children iframes from parent document, but doing this trick to find parent within iframe... you've got me

    Thank you for help, I belive this will work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width