Calling javascript in other window?

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

    #1

    Calling javascript in other window?

    I have two windows in a frame. I want to be able that each can open a
    pop up window and that the handle to that window can be stored
    somewhere, so that each can talk to the pop up.

    is it possible that window 1 in a frameset calls a function in window 2,
    ie. something like:

    window 1:

    ....
    opener()
    ....
    write_to_pop_up ('hello, world');


    window 2:

    handle; // global handle

    function opener() {

    handle = window.open(... );
    }

    function write_to_pop_up (text) {

    handle.write(te xt);
    }


    Since both reside in a frameset, how can I check that window 2 has been
    fully loaded? I.e. that I really can call opener?

    Any good pointers to information like this? Or a good advanced JS
    tutorial?

    TIA,

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
  • Michael Winter

    #2
    Re: Calling javascript in other window?

    On 8 Sep 2004 03:33:22 GMT, John Bokma <postmaster@cas tleamber.com> wrote:

    [snip]
    [color=blue]
    > window 2:
    >
    > handle; // global handle[/color]

    That in itself will cause an error. Always use var to declare variables,
    but note that using var affects scope: when used in a function, the
    variable will be local.

    var global = ...

    function myFunction() {
    var local = ...
    }
    [color=blue]
    > function opener() {
    >
    > handle = window.open(... );
    > }[/color]

    I wouldn't advise naming your function, opener. A property by that name
    already exists, and you'll function will overwrite it.

    [snip]
    [color=blue]
    > Since both reside in a frameset, how can I check that window 2 has been
    > fully loaded? I.e. that I really can call opener?[/color]

    You could place code that sets a boolean and will execute once the
    document has loaded. You can poll this variable and wait until it's set to
    true before accessing the contents of the document.
    [color=blue]
    > Any good pointers to information like this? Or a good advanced JS
    > tutorial?[/color]

    I'd start with the group FAQ. It contains the answer you're looking for,
    as well as links to other information sources. However, participating in
    this group is another way to learn things that you aren't likely to pick
    up elsewere.

    <URL:http://jibbering.com/faq/>

    Good luck
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Robert

      #3
      Re: Calling javascript in other window?

      John Bokma <postmaster@cas tleamber.com> wrote in message news:<Xns955DE5 69CE13castleamb er@130.133.1.4> ...[color=blue]
      > I have two windows in a frame. I want to be able that each can open a
      > pop up window and that the handle to that window can be stored
      > somewhere, so that each can talk to the pop up.
      >[/color]

      I believe that the following code will demonstrate what coding is
      needed to do what you indicated.

      The only minor problems is that I gave up with getting the color set
      when I closed the popup and brought the popup back again. In this
      case, you have to set another color before the color will change. If
      you change the color after you close the popup and bring the popup
      back, the color will be set.

      Robert

      frames.html
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Frames Layout</title>
      </head>
      <frameset rows="50%,50%" >
      <frame src="frame1.htm l"
      id="myframe1"
      name="nameframe 1"
      scrolling=yes>
      <frame src="frame2.htm l"
      id="myframe2"
      name="nameframe 2"
      scrolling=yes>
      </frameset>
      </html>


      frome1.html

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Frame 1</title>
      <script type="text/javascript">

      function invokeDisplayCo lor()
      {
      var theForm = document.forms["myForm"];
      displayColor(th eForm,"color1") ;
      }


      function displayColor (form,theColor)
      {
      var colorList = ["green","blue", "red","blac k"];

      if (top.frames["nameframe2 "].popupOpened)
      {
      var boxColor = colorList[form.elements[theColor].value];
      var popupWindow = top.frames["nameframe2 "].newWindow;
      popupWindow.cha ngeColor(boxCol or);
      }
      else
      {
      setTimeout("inv okeDisplayColor ()",500);
      }
      }
      </script>
      </head>

      <body onload="invokeD isplayColor();" >
      <p>This is frame 1.</p>
      <form name="myForm">
      <p>The color will be set when the popup
      appears the first time. When the popup is active, the color will
      be changed immediately.</p>
      <p>Pick a color. </p>
      <SELECT name="color1"
      style="WIDTH: 180px"
      onchange="displ ayColor(this.fo rm,this.name);" >
      <OPTION value = "0" >Green
      <OPTION value = "1" >Blue
      <option value = "2" >Red
      <option value = "3" >Black
      </OPTION>
      </SELECT>
      </form>
      </body>
      </html>

      frame2.html

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Frame 2</title>
      <script type="text/javascript">

      var newWindow;
      var popupOpened = false;

      function openWindow ()
      {
      newWindow = window.open(
      "popup.html ",
      "popup",
      "scrollbars,res izable,location ,width=500,heig ht=300");
      }

      </script>
      </head>

      <body>
      <p>This is frame 2.</p>
      <form>
      <input type="button"
      name="activate"
      value="Open the popup."
      onclick="openWi ndow();">
      <br><br><br>
      <input type="button"
      name="deactivat e"
      value="Close the popup."
      onclick="if (popupOpened) {newWindow.clos e();}">
      </form>
      </body>
      </html>


      popup.html

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Popup</title>
      <SCRIPT type="text/javascript">
      function changeColor(tex tColor)
      {
      document.getEle mentById("color it").style.colo r = textColor;
      }

      </script>
      </head>
      <body
      onload="opener. popupOpened = true;"
      onunload="opene r.popupOpened = false;">
      <p>This is the popup.</p>
      <p id="colorit">Ch ange the color of this text.</p>
      </body>
      </html>

      Comment

      • Robert

        #4
        Re: Calling javascript in other window?

        rccharles@my-deja.com (Robert) wrote in message[color=blue]
        > The only minor problems is that I gave up with getting the color set
        > when I closed the popup and brought the popup back again.[/color]

        I was able to add the code to do this.

        The file frames.html divides the window into two frames. These are
        frame1.html and frame2.html. When the user presses a button,
        frame2.html creates a popup window via popup.html. When popup.html is
        closed, it needs to tell frame1.html that frame1.html needs to monitor
        for another appearance of popup.html. I could not figure out a way of
        directly access frame1.html. I had to place a function in frame2.html
        which invokes a function in frame1.html.

        How would I directly invoke a function in frame1.html from popup.html?

        Robert

        Here are the routines:

        frames.html

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <title>Frames Layout</title>
        </head>
        <frameset rows="50%,50%" >
        <frame src="frame1.htm l"
        id="myframe1"
        name="nameframe 1"
        scrolling=yes>
        <frame src="frame2.htm l"
        id="myframe2"
        name="nameframe 2"
        scrolling=yes>
        </frameset>
        </html>


        frome1.html

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <title>Frame 1</title>
        <script type="text/javascript">

        function invokeDisplayCo lor()
        {
        var theForm = document.forms["myForm"];
        displayColor(th eForm,"color1") ;
        }


        function displayColor (form,theColor)
        {
        var colorList = ["green","blue", "red","blac k"];

        if (top.frames["nameframe2 "].popupOpened)
        {
        var boxColor = colorList[form.elements[theColor].value];
        var popupWindow = top.frames["nameframe2 "].newWindow;
        popupWindow.cha ngeColor(boxCol or);
        }
        else
        {
        setTimeout("inv okeDisplayColor ()",500);
        }
        }
        </script>
        </head>

        <body onload="invokeD isplayColor();" >
        <p>This is frame 1.</p>
        <form name="myForm">
        <p>Select which color you wish to see in the second line of text
        in the popup.</p>
        <SELECT name="color1"
        style="WIDTH: 180px"
        onchange="displ ayColor(this.fo rm,this.name);" >
        <OPTION value = "0" >Green
        <OPTION value = "1" >Blue
        <option value = "2" >Red
        <option value = "3" >Black
        </OPTION>
        </SELECT>
        </form>
        </body>
        </html>


        frame2.html

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <title>Frame 2</title>
        <script type="text/javascript">

        var newWindow;
        var popupOpened = false;

        function openWindow ()
        {
        newWindow = window.open(
        "popup.html ",
        "popup",
        "scrollbars,res izable,location ,width=500,heig ht=300");
        }

        function startChangeColo r()
        {
        top.frames["nameframe1 "].invokeDisplayC olor();
        }

        </script>
        </head>

        <body>
        <p>This is frame 2.</p>
        <form>
        <input type="button"
        name="activate"
        value="Open the popup."
        onclick="openWi ndow();">
        <br><br><br>
        <input type="button"
        name="deactivat e"
        value="Close the popup."
        onclick="if (popupOpened) {newWindow.clos e();}">
        </form>
        </body>
        </html>


        popup.html

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <title>Popup</title>
        <SCRIPT type="text/javascript">
        function changeColor(tex tColor)
        {
        document.getEle mentById("color it").style.colo r = textColor;
        }

        </script>
        </head>
        <body
        onload="opener. popupOpened = true;"
        onunload="if(op ener)
        {opener.popupOp ened = false;
        opener.startCha ngeColor();}">
        <p>This is the popup.</p>
        <p id="colorit">Ch ange the color of this text.</p>
        </body>
        </html>

        Comment

        Working...