Referencing windows by name?

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

    #16
    Re: Referencing windows by name? -- now a Working Opener and Callback

    Robert Mark Bram wrote:
    [color=blue]
    > Howdy All!
    >
    > I finally worked this out. Thanks very much to DU, who I hope will excuse me
    > for posting the sloppy html below - the only thing I want to show here is
    > the valid EcmaScript required to do the job.
    >
    > This example involves two windows - a menu window and a content window.
    > Clicking links from the menu window open their documents in the content
    > window, loading only when appropriate. In turn, each content page has a link
    > back to the menu window, again, only loading where appropriate.
    >
    > Any comments, criticisms or corrections welcome!
    >
    > Rob
    > :)
    >
    > ==========
    > menu.htm
    > ==========
    > <html><head>
    > <script type="text/javascript">
    >
    > window.name = "menuWindow ";[/color]

    I don't think this is needed; right now, I don't see this as necessary
    nor recommendable. menu.htm can be referred as the opener all the time;
    opener is a pointer, a window object reference while window.name is just
    a string property of the window object only useful in conjunction with
    the HTML 4 target attribute.
    [color=blue]
    > var contentWindow;
    > var previousContent Url;
    >
    > // Menu window uses this function to open a content window.
    > function openContentWind ow (contentUrl)
    > {
    > if (contentWindow == null ||
    > contentWindow.c losed)
    > {
    > contentWindow = window.open (contentUrl, "contentWindow" );
    > } // end if
    > else if (previousConten tUrl != contentUrl)
    > {
    > contentWindow = window.open (contentUrl, "contentWindow" );
    > contentWindow.f ocus();
    > } // end else if
    > else
    > {
    > contentWindow.f ocus();
    > } // end else
    >
    > previousContent Url = contentUrl;
    > } // end openContentWind ow function
    > </script>
    > </head><body>
    > <h1>Menu:</h1>
    > <p><a href="content1. htm"
    > onClick="openCo ntentWindow(thi s.href); return false;">Content 1</a></p>
    > <p><a href="content2. htm"
    > onClick="openCo ntentWindow(thi s.href); return false;">Content 2</a></p>
    > </body>
    > </html>
    >
    >
    > ==========
    > content1.htm
    > ==========
    > <html><head>
    > <script type="text/javascript" src="windowCode .js"></script>
    > </head><body>
    > <h1>Content 1</h1>
    > <p><a href="menu.htm"
    > onClick="showMe nuWindowFromCon tentWindow(this .href); return
    > false;">Menu</a></p>
    > </body></html>
    >
    > ==========
    > content2.htm
    > ==========
    > <html><head>
    > <script type="text/javascript" src="windowCode .js"></script>
    > </head><body>
    > <h1>Content 2</h1>
    > <p><a href="menu.htm"
    > onClick="showMe nuWindowFromCon tentWindow(this .href); return
    > false;">Menu</a></p>
    > </body></html>
    >
    >
    > ==========
    > windowCode.htm
    > ==========
    > function showMenuWindowF romContentWindo w (pageUrl)
    > {
    > // Either gain reference to a window nameed menuWindow
    > // or open a blank window named menuWindow.
    > menuWindow = window.open('', 'menuWindow');[/color]

    Right here, I think you need to explain your logic a bit. Why would you
    want to load the opener (menuWindow) into the content window? This does
    not strike to me as natural, recommendable.. .
    [color=blue]
    >
    > // If it is a blank window then its location will be blank
    > // and should be reloaded.
    > if (String (menuWindow.loc ation) != pageUrl)[/color]

    This line above looks ok but for debugging purposes, it's preferable to use
    if (String (menuWindow.loc ation.href) != pageUrl)
    This way of coding is more reliable.

    Also, the previous line just opened an empty, "about:blan k" document:
    so, menuWindow.loca tion.href will always be different from the pageUrl.
    I'm a bit confused on the purpose of your page. Why do you absolutely
    need to use 3 windows (1 opener and 2 popups)?
    [color=blue]
    > {
    > menuWindow.loca tion.replace (pageUrl);
    > } // end if
    > else
    > {
    > menuWindow.focu s();
    > } // end else
    > } // end openMenuWindow function
    >
    >[/color]

    menuWindow should be declared outside that function as a global
    variable: just var menuWindow

    Finally, you can add these lines in your top part of your documents to
    trigger standards compliance rendering mode in MSIE 6 for windows:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">

    <html lang="en-us">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Language" content="en-us">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">

    <title>Title goes here</title>

    DU

    Comment

    • Robert Mark Bram

      #17
      Re: Referencing windows by name? -- now a Working Opener and Callback

      Hi DU!
      [color=blue][color=green]
      > > ==========
      > > menu.htm
      > > ==========
      > > <html><head>
      > > <script type="text/javascript">
      > >
      > > window.name = "menuWindow ";[/color]
      >
      > I don't think this is needed; right now, I don't see this as necessary
      > nor recommendable. menu.htm can be referred as the opener all the time;[/color]

      Not if menu.htm was closed or if the content window was not reached through
      menu.htm.

      So the idea is that if 'menu' is clicked on a content window and menu.htm
      isn't open, it will be opened. In this case, opener will be null.
      [color=blue]
      > opener is a pointer, a window object reference while window.name is just
      > a string property of the window object only useful in conjunction with
      > the HTML 4 target attribute.[/color]

      When you open a window like this:
      window.open ('', "menuWindow ");
      it will search for a window named 'menuWindow' and return reference to it or
      open a blank window newly named 'menuWindow' and return reference to that.
      This line in menu.htm:
      window.name = "menuWindow ";
      makes sure that the menu window has that name if it was the first window
      opened..
      [color=blue][color=green]
      > > ==========
      > > windowCode.htm[/color][/color]

      This should have been "windowCode .js"
      [color=blue][color=green]
      > > ==========
      > > function showMenuWindowF romContentWindo w (pageUrl)
      > > {
      > > // Either gain reference to a window nameed menuWindow
      > > // or open a blank window named menuWindow.
      > > menuWindow = window.open('', 'menuWindow');[/color]
      >
      > Right here, I think you need to explain your logic a bit. Why would you
      > want to load the opener (menuWindow) into the content window? This does
      > not strike to me as natural, recommendable.. .[/color]

      It isn't loading menuWindow into the content window. If the first parameter
      (url) is empty and the second (window name) has a value, it will do one of
      two things:
      - return reference to an already open window with that name
      - open a blank window with that name and return a reference to it
      This is important because if the contentWindow is already open, I only want
      to change its contents if it does not have the corrent document loaded.
      [color=blue][color=green]
      > > // If it is a blank window then its location will be blank
      > > // and should be reloaded.
      > > if (String (menuWindow.loc ation) != pageUrl)[/color]
      >
      > This line above looks ok but for debugging purposes, it's preferable to[/color]
      use[color=blue]
      > if (String (menuWindow.loc ation.href) != pageUrl)
      > This way of coding is more reliable.[/color]

      Thanks!
      [color=blue]
      > Also, the previous line just opened an empty, "about:blan k" document:[/color]

      Only if no window with the desired name was found - in which case it will
      have its location property manipulated to load the correct window..
      [color=blue]
      > so, menuWindow.loca tion.href will always be different from the pageUrl.
      > I'm a bit confused on the purpose of your page. Why do you absolutely
      > need to use 3 windows (1 opener and 2 popups)?[/color]

      I am not. There are only two windows.
      [color=blue]
      >[color=green]
      > > {
      > > menuWindow.loca tion.replace (pageUrl);
      > > } // end if
      > > else
      > > {
      > > menuWindow.focu s();
      > > } // end else
      > > } // end openMenuWindow function
      > >
      > >[/color]
      >
      > menuWindow should be declared outside that function as a global
      > variable: just var menuWindow[/color]

      Ok. :)
      [color=blue]
      >
      > Finally, you can add these lines in your top part of your documents to
      > trigger standards compliance rendering mode in MSIE 6 for windows:
      >
      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      > "http://www.w3.org/TR/html4/strict.dtd">
      >
      > <html lang="en-us">
      >
      > <head>
      >
      > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      > <meta http-equiv="Content-Language" content="en-us">
      > <meta http-equiv="Content-Style-Type" content="text/css">
      > <meta http-equiv="Content-Script-Type" content="text/javascript">
      >
      > <title>Title goes here</title>[/color]

      And ok again!

      Thank you once more DU, for the time you have taken.

      Rob
      :)


      Comment

      Working...