JS in frame1 <--> JS in frame2

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

    JS in frame1 <--> JS in frame2

    I have a page with two frames. Frame1 contains an inline script. Frame2
    contains inline and 2 loaded scripts. How may I call a function in
    frame1(inline) from frame2(inline or loaded)?

    Albert

  • Lasse Reichstein Nielsen

    #2
    Re: JS in frame1 &lt;--&gt; JS in frame2

    Albert Wagner <alwagner@tcac. net> writes:
    [color=blue]
    > I have a page with two frames. Frame1 contains an inline script. Frame2
    > contains inline and 2 loaded scripts. How may I call a function in
    > frame1(inline) from frame2(inline or loaded)?[/color]

    You have one page, which is a frameset. It contains two other pages
    in frames.

    To access a global variable of frame1 from frame2, you just write (in
    frame2):

    parent.frames['frame1'].globalVarName

    <URL:file://localhost/D:/Home/lrn/html/faq/JSwindows.html# ref_3_12>

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Cornford

      #3
      Re: JS in frame1 &lt;--&gt; JS in frame2

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:oexrkldu.f sf@hotpop.com.. .
      <snip>[color=blue]
      ><URL:file://localhost/D:/Home/lrn/html/faq/JSwindows.html# ref_3_12>[/color]

      Did you mean:-

      <URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >

      Richard.


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: JS in frame1 &lt;--&gt; JS in frame2

        "Richard Cornford" <richard@litote s.demon.co.uk> writes:
        [color=blue]
        > "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        > news:oexrkldu.f sf@hotpop.com.. .[color=green]
        > ><URL:file://localhost/D:/Home/lrn/html/faq/JSwindows.html# ref_3_12>[/color]
        >
        > Did you mean:-
        > <URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >[/color]

        DOH!
        Thanks.

        /L 'Must. Be. More. Careful!'
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Richard Cornford

          #5
          Re: JS in frame1 &lt;--&gt; JS in frame2

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:k78fkkd6.f sf@hotpop.com.. .
          <snip>[color=blue][color=green]
          > > Did you mean:-
          > > <URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >[/color]
          >
          > DOH!
          > Thanks.[/color]

          Incidentally, I liked the page, though you could have mentioned browsers
          that lack the window.open function (PDA and embedded) in the "Problems
          with opening windows" section.

          Richard.


          Comment

          • Albert Wagner

            #6
            Re: JS in frame1 &lt;--&gt; JS in frame2

            On 12 Sep 2003 00:31:25 +0200
            Lasse Reichstein Nielsen <lrn@hotpop.com > wrote:
            <snip>[color=blue]
            > You have one page, which is a frameset. It contains two other pages
            > in frames.
            >
            > To access a global variable of frame1 from frame2, you just write (in
            > frame2):
            >
            > parent.frames['frame1'].globalVarName
            >
            > <URL: file://localhost/D:/Home/lrn/html/faq/JSwindows.html# ref_3_12 >[/color]

            Thank you, Lasse, for both your exellent reply and the link. I've been
            saveing all the good links from this NG.

            Comment

            • Jim Ley

              #7
              Re: JS in frame1 &lt;--&gt; JS in frame2

              On Thu, 11 Sep 2003 23:02:51 +0000 (UTC), "Richard Cornford"
              <richard@litote s.demon.co.uk> wrote:
              [color=blue]
              >Incidentally , I liked the page, though you could have mentioned browsers
              >that lack the window.open function (PDA and embedded) in the "Problems
              >with opening windows" section.[/color]

              One of the interesting things in opera 6.0 on Series 60 Symbian
              devices is that javascript defaults to OFF, but pop-ups default to on,
              which seems kind of odd.

              (I hope to be looking further into Opera/Netfront and others on Series
              60 phones so, both claim DOM/DHTML)

              Jim.
              --
              comp.lang.javas cript FAQ - http://jibbering.com/faq/

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: JS in frame1 &lt;--&gt; JS in frame2

                "Richard Cornford" <richard@litote s.demon.co.uk> writes:
                [color=blue]
                > Incidentally, I liked the page,[/color]

                Thanks!
                [color=blue]
                > though you could have mentioned browsers
                > that lack the window.open function (PDA and embedded) in the "Problems
                > with opening windows" section.[/color]

                Consider it done and uploaded.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Albert Wagner

                  #9
                  Re: JS in frame1 &lt;--&gt; JS in frame2

                  On 12 Sep 2003 00:31:25 +0200
                  Lasse Reichstein Nielsen <lrn@hotpop.com > wrote:
                  <snip>

                  I tried your suggestion and the link, but no joy. I have included the
                  code and the error. Any criticisms or advice are welcome. Thanks
                  ahead.

                  Albert

                  ========== test.html =============== =============== ===========
                  <HTML>
                  <HEAD>
                  <TITLE>Test and Debug</TITLE>
                  </HEAD>
                  <FRAMESET ROWS="30%,70%" COLS="30%,70%">

                  <FRAME SRC="debugger.h tml" NAME="debugFram e" ID="debugFrame" >
                  <FRAME SRC="program.ht ml" NAME="programFr ame" ID="programFram e">

                  </FRAMESET>
                  </HTML>

                  =========== debugger.html =============== =============== ======
                  <HTML>
                  <HEAD>
                  <TITLE>Debugger </TITLE>
                  <SCRIPT type="text/JavaScript">

                  var DebugPrint = function(_line) {
                  debugFrame.docu ment.writeln("< p>"+_line+"</p>");
                  }

                  function Initialize() {
                  debugFrame = window.parent.f rames.debugFram e;
                  DebugPrint("Deb ugger started.");
                  }

                  </SCRIPT>
                  </HEAD>
                  <BODY onLoad="Initial ize()">
                  </BODY>
                  </HTML>

                  =========== program.html =============== =============== ==========
                  <html>
                  <head>
                  <title>JavaScri pt-Test</title>
                  <script type="text/javascript">
                  var Print = function(_line) {
                  programFrame.do cument.writeln( '<br>'+_line+' </br>');
                  }

                  function Initialize() {
                  programFrame = window.parent.f rames.programFr ame;
                  Print("Program started.");
                  window.parent.f rames.debugFram e.DebugPrint("F rom Program"); // error
                  this line
                  }
                  </script>
                  </head>
                  <body onLoad="Initial ize()">
                  </body>
                  </html>

                  ============ JavaScript Console / Opera 7.11 =============== ========

                  Event thread: onload
                  Error:
                  name: TypeError
                  message: Statement on line 9: Expression did not evaluate to a function
                  object: window.parent.f rames.debugFram e.DebugPrint
                  Backtrace:
                  Line 9 of inline#1 script in
                  file://localhost/usr/js/work/mover/program.html
                  window.parent.f rames.debugFram e.DebugPrint("F rom Program");
                  In unknown script
                  Initialize();
                  At unknown location
                  {event handler trampoline}

                  Comment

                  • Vjekoslav Begovic

                    #10
                    Re: JS in frame1 &lt;--&gt; JS in frame2

                    "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote
                    [color=blue][color=green]
                    > > <URL: http://www.infimum.dk/HTML/JSwindows.html#ref_3_12 >[/color]
                    >
                    > DOH!
                    > Thanks.[/color]

                    It says:

                    "Referencin g new windows
                    The "window.ope n" function returns a reference to the new window, or to the
                    old window, if it was already open. It is important to keep this reference
                    in order to communicate with the new window, or to close it again.
                    var myWindow = document.open(" foo.html","wind owName","resiza ble=no"); "

                    Shouldn't be "window.ope n"?

                    And you have a typo here:
                    "Since "window" is a reference to the global object, the function can also
                    be referred to as just the global variabel "open"."

                    BTW, nice page. Thanks.

                    Vjekoslav


                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: JS in frame1 &lt;--&gt; JS in frame2

                      Albert Wagner <alwagner@tcac. net> writes:
                      [color=blue]
                      > <FRAME SRC="debugger.h tml" NAME="debugFram e" ID="debugFrame" >
                      > <FRAME SRC="program.ht ml" NAME="programFr ame" ID="programFram e">[/color]

                      I recommend against having the same "name" and "id" attribute values.

                      In both HTML and XHTML, "name" is a valid attribute name for frames.
                      If you need the "id" attribute, add "Id" to the end of it, or something
                      similar.

                      [color=blue]
                      > =========== debugger.html =============== =============== ======[/color]
                      [color=blue]
                      > var DebugPrint = function(_line) {
                      > debugFrame.docu ment.writeln("< p>"+_line+"</p>");[/color]

                      If we are in the debugFrame, then it suffices to write
                      document.writel n(...);
                      The scope rules will garantee that we always refer to the current
                      frame's document object

                      You should escape the "</" token. The recommended way is "<\/".

                      I.e.:
                      document.writel n("<p>"+_line+" <\/p>");
                      [color=blue]
                      > }
                      >
                      > function Initialize() {
                      > debugFrame = window.parent.f rames.debugFram e;[/color]

                      This is in the debugFrame, so "debugFrame = window" should be
                      equivalent.
                      [color=blue]
                      > DebugPrint("Deb ugger started.");[/color]

                      Here you make a document.write to the current document. That completely
                      erases the current document and starts writing a new one. Erasing the
                      current document also loses the debugFrame function. So, if this page
                      loads faster than the program page, then it erases the function before
                      it can be called.
                      [color=blue]
                      > =========== program.html =============== =============== ==========[/color]
                      [color=blue]
                      > var Print = function(_line) {
                      > programFrame.do cument.writeln( '<br>'+_line+' </br>');[/color]

                      As above. Change "</br>" to "<\/br>". The "programFra me." isn't
                      necessary ...
                      [color=blue]
                      > }
                      >
                      > function Initialize() {
                      > programFrame = window.parent.f rames.programFr ame;[/color]

                      .... and this can be shorter (or omitted)
                      [color=blue]
                      > Print("Program started.");[/color]

                      Here you erase the current page. The running function isn't lost before
                      it finishes, so the next line is executed.
                      [color=blue]
                      > window.parent.f rames.debugFram e.DebugPrint("F rom Program"); // error
                      > this line[/color]

                      If this frame loads faster than the debug frame, then the function
                      isn't available yet. If it is slower, the debug frame erases it again.

                      There is a very narrow margin where this could possibly work.
                      [color=blue]
                      > message: Statement on line 9: Expression did not evaluate to a function
                      > object: window.parent.f rames.debugFram e.DebugPrint[/color]

                      .... and therefore it most likely fails every time.

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: JS in frame1 &lt;--&gt; JS in frame2

                        "Vjekoslav Begovic" <vjbegovic@inet .hr> writes:
                        [color=blue]
                        > Shouldn't be "window.ope n"?[/color]

                        It should indeed. Thanks.
                        [color=blue]
                        > And you have a typo here:
                        > "Since "window" is a reference to the global object, the function can also
                        > be referred to as just the global variabel "open"."[/color]

                        I think it is correct. The sentence could be better though, so I have
                        done a rewrite.
                        [color=blue]
                        > BTW, nice page.[/color]

                        Thanks. Any other typos you should find, are welcome too

                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Vjekoslav Begovic

                          #13
                          Re: JS in frame1 &lt;--&gt; JS in frame2

                          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                          news:r82m746m.f sf@hotpop.com.. .[color=blue]
                          > "Vjekoslav Begovic" <vjbegovic@inet .hr> writes:[/color]
                          [color=blue][color=green]
                          > > And you have a typo here:
                          > > "Since "window" is a reference to the global object, the function can[/color][/color]
                          also[color=blue][color=green]
                          > > be referred to as just the global variabel "open"."[/color]
                          >
                          > I think it is correct.[/color]

                          My english could be much better, but I think "variabel" should be
                          "variable".

                          Regards

                          Vjekoslav


                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: JS in frame1 &lt;--&gt; JS in frame2

                            "Vjekoslav Begovic" <vjbegovic@inet .hr> writes:
                            [color=blue]
                            > My english could be much better, but I think "variabel" should be
                            > "variable".[/color]

                            So could mine, and you are correct. The eyes are the first place you
                            go blind :)

                            /L 'I *so* need to install that spelling checker again'
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • Albert Wagner

                              #15
                              Re: JS in frame1 &lt;--&gt; JS in frame2

                              On 12 Sep 2003 10:52:45 +0200
                              Lasse Reichstein Nielsen <lrn@hotpop.com > wrote:
                              <snip>

                              Hi, Lasse. Thanks for another very helpful post. As you might have
                              guessed, I am not only a newbie to JavaScript but also to HTML. Based
                              on your remarks I have rewritten my code and it now appears to work. I
                              have included it below. If you have the time, I would appreciate
                              another critique. Thanks again.

                              Albert

                              =============== test.html =============== ====
                              <HTML>
                              <HEAD>
                              <TITLE>Test and Debug</TITLE>
                              </HEAD>
                              <FRAMESET ROWS="30%,70%" COLS="30%,70%">

                              <FRAME SRC="debugger.h tml" NAME="debugFram e" ID="debugFrameI d">
                              <FRAME SRC="program.ht ml" NAME="programFr ame" ID="programFram eId">

                              </FRAMESET>
                              </HTML>

                              =============== debugger.html =============== =====
                              <HTML>
                              <HEAD>
                              <TITLE>Debugger </TITLE>
                              <SCRIPT type="text/JavaScript">

                              var listContents = "";

                              function PrintLine(_line ) {
                              listContents += _line + "\n";
                              RefreshList();
                              }

                              function RefreshList() {
                              document.theFor m.debugList.foc us();
                              document.theFor m.debugList.val ue = listContents;
                              document.theFor m.debugList.blu r();
                              }

                              function Initialize() {
                              PrintLine("Debu gger loaded...");
                              }

                              </SCRIPT>
                              </HEAD>
                              <BODY onLoad="Initial ize()">
                              <FORM name="theForm">
                              <TEXTAREA name="debugList " cols="40"
                              rows="500" readonly
                              onChange = "RefreshLis t">
                              </TEXTAREA>
                              </FORM>
                              </BODY>
                              </HTML>

                              ============== program.html =============== ==============
                              <html>
                              <head>
                              <title>Progra m</title>
                              <script type="text/javascript">

                              var debug;

                              function Initialize() {
                              debug = parent.frames.d ebugFrame.Print Line;
                              debug("Program loaded...");
                              RunProgram();
                              }

                              function RunProgram() {
                              if (debug) debug("RunProgr am function entered");
                              if (debug) debug("debug message 1");
                              if (debug) debug("debug message 2");
                              }

                              </script>
                              </head>
                              <body onLoad="Initial ize()">
                              </body>
                              </html>


                              Comment

                              Working...