Referencing windows by name?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    Referencing windows by name?

    Hi All!

    Is there a way to reference a window by name without doing something like
    this:
    open (, 'windowName');
    The open method will open a blank window if there is no window with such a
    name.

    I am trying to organise a navigation structure between two windows with
    content from the same host.. I have been trying the following:

    The 'content pages' link back to the menu with this:
    <a href="main.asp" onClick="return openMenuWindow( this.href);" >enter</a>

    And the 'menu page' links to various content pages with this:
    <a href="content.a sp" onClick="return openContentWind ow(this.href);"[color=blue]
    >enter</a>[/color]

    The functions are below.My latest problem is that the links are opening up
    in the same page, rather than two pages. However, I have a sneaking
    suspicion that I am trying too hard and the correct technique is much
    simpler.

    Any advice on how to get these two windows talking to each other would be
    most welcome!

    var contentWindow = null;
    // Menu window should use this function to open a content window.
    function openContentWind ow (pageUrl)
    {
    if (contentWindow != null &&
    String(contentW indow.location) == pageUrl)
    {
    contentWindow.f ocus();
    return false;
    } // end if
    contentWindow = window.open (pageUrl, "contentWindow" );
    contentWindow.m enuWindow = window;
    contentWindow.f ocus();
    } // end openContentWind ow function



    // Content window should use this function to open a menu window.
    function openMenuWindow (pageUrl)//(pageUrl, pageName, w, h, scroll, pos)
    {
    if (window.menuWin dow != null &&
    !window.menuWin dow.closed)
    {
    window.open (, 'main').focus() ;
    return;
    } // end if

    var pageName = 'main';
    var w = '800';
    var h = '450';
    var scroll = 'no';
    var pos = 'center';

    window.name="co ntentWindow";
    var LeftPosition=(s creen.width)?(s creen.width-w)/2:100;
    var TopPosition=(sc reen.height)?(s creen.height-h)/2:100;

    var settings=
    'width='+w+
    ',height='+h+
    ',top='+TopPosi tion+
    ',left='+LeftPo sition+
    ',scrollbars='+ scroll+

    ',location=no,d irectories=no,s tatus=no,menuba r=no,toolbar=no ,resizable=no';

    var menuWindow = window.open (pageUrl, pageName, settings);
    menuWindow.focu s();
    return false;
    } // end openContentWind ow function

    Rob
    :)


  • Thomas 'PointedEars' Lahn

    #2
    Re: Referencing windows by name?

    Robert Mark Bram wrote:
    [color=blue]
    > Is there a way to reference a window by name without doing something like
    > this:
    > open (, 'windowName');
    > The open method will open a blank window if there is no window with such a
    > name.[/color]

    var w = window.open('', 'windowName', 'featuresIfNotD efaults');
    if (w && !w.closed)
    {
    ...
    }


    PointedEars
    --
    apprentice.c - parses /etc/magic to learn magic

    (from the file-3.40 README)

    Comment

    • Robert Mark Bram

      #3
      Re: Referencing windows by name?

      Hi Thomas!
      [color=blue]
      > var w = window.open('', 'windowName', 'featuresIfNotD efaults');
      > if (w && !w.closed)
      > {
      > ...
      > }[/color]

      But this opens a blank window if no window with the desired name is found,
      which is not what I want.

      I want a way to find out if the named window is there, and change focus to
      it if it is. If it is not, I wish to make the window anew - but not a blank
      one!

      Rob
      :)


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Referencing windows by name?

        Robert Mark Bram wrote:
        [color=blue][color=green]
        >> var w = window.open('', 'windowName', 'featuresIfNotD efaults');
        >> if (w && !w.closed)
        >> {
        >> ...
        >> }[/color]
        >
        > But this opens a blank window if no window with the desired name is found,
        > which is not what I want.[/color]

        You are right. Sorry, misunderstood that.
        [color=blue]
        > I want a way to find out if the named window is there, and change focus to
        > it if it is. If it is not, I wish to make the window anew - but not a blank
        > one![/color]

        The above code allows for getting a reference to the new window. For that
        reference to the window to be accessible by another window, you need to
        include this information in the context of the latter. The easy way is to
        add a property:

        w = window.open(... );
        ...
        w2 = window.open(... );
        ...
        if (w && !w.closed && w2 && !w2.closed)
        {
        w2.w = w;
        w.w2 = w2;
        }

        In `w' you then can access `w2' with `self.w2' or `window.w2' as well
        as you can access `w' in `w2' with `self.w' or `window.w', provided
        that `self' or `window' references the current window object in the
        DOM you are using (as it is in all well-known browsers.)

        You could also access the other window by referencing it as property
        of the opener window until the latter is still there and if you use
        global variables (as long as the above can be provided). In `w':

        if (opener && !opener.closed && opener.w2 && !opener.w2.clos ed)
        {
        ...
        }
        else
        {
        ...
        }

        And in `w2' respectively:

        if (opener && !opener.closed && opener.w && !opener.w.close d)
        {
        ...
        }
        else
        {
        ...
        }

        As posted many times before (Google is your friend!), without references
        you cannot access other windows. There is no collection of Window objects
        (unless you implement one.)


        HTH

        PointedEars

        Comment

        • Robert Mark Bram

          #5
          Re: Referencing windows by name?

          Hi Thomas.

          Your advice is pretty much what I originally tried. Here is a test scenario
          for that. My problem is that on each alert, the 'placed' window property is
          undefined!

          I am getting really frustrated here. Any advice would be most welcome!

          Rob
          :)

          ============
          menu.htm

          ============
          <html><head>
          <script language="JavaS cript" type="text/javascript"
          src="windowCode .js"></script>
          </head><body>
          <p>Menu:</p>
          <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 language="JavaS cript" type="text/javascript"
          src="windowCode .js"></script>
          </head><body>
          <p>Content 1</p>
          <p><a href="menu.htm"
          onClick="openMe nuWindow(this.h ref); return false;">Menu</a></p>
          </body></html>

          ============
          content2.htm

          ============
          <html><head>
          <script language="JavaS cript" type="text/javascript"
          src="windowCode .js"></script>
          </head><body>
          <p>Content 2</p>
          <p><a href="menu.htm"
          onClick="openMe nuWindow(this.h ref); return false;">Menu</a></p>
          </body></html>


          ============
          windowCode.js

          ============
          // Menu window uses this function to open a content window.
          function openContentWind ow (pageUrl)
          {
          var win = null;


          alert ("window.conten tWindow = " + window.contentW indow);

          if (window.content Window &&
          !window.content Window.closed)
          {
          win = window.contentW indow;
          if (String(win.loc ation) != pageUrl)
          {
          win.location.re place (pageUrl);
          } // end if
          } // end if
          else
          {
          win = window.open (pageUrl, "contentWindow" );
          window.contentW indow = win;
          } // end else
          win.menuWindow = self;
          win.focus();
          } // end openContentWind ow function




          // Content windows use this function to open a menu window.
          function openMenuWindow (pageUrl)//(pageUrl, pageName, w, h, scroll, pos)
          {
          window.name="co ntentWindow";

          var win = null;

          alert ("self.menuWind ow = " + self.menuWindow );

          if (window.menuWin dow &&
          !window.menuWin dow.closed)
          {
          win = window.menuWind ow;
          if (String(win.men uWindow) != pageUrl)
          {
          win.location.re place (pageUrl);
          } // end if
          } // end if
          else
          {
          win = newMenuWindow (pageUrl);
          window.menuWind ow = win;
          } // end else

          win.contentWind ow = self;
          win.focus();
          } // end openMenuWindow function


          function newMenuWindow (pageUrl)
          {
          var pageName = 'menuWindow';
          var w = '800';
          var h = '450';
          var scroll = 'no';
          var pos = 'center';

          var LeftPosition=(s creen.width)?(s creen.width-w)/2:100;
          var TopPosition=(sc reen.height)?(s creen.height-h)/2:100;

          var settings=
          'width='+w+
          ',height='+h+
          ',top='+TopPosi tion+
          ',left='+LeftPo sition+
          ',scrollbars='+ scroll+

          ',location=no,d irectories=no,s tatus=no,menuba r=no,toolbar=no ,resizable=no';

          var menuWindow = window.open (pageUrl, pageName, settings);
          return menuWindow;
          } // end openContentWind ow function


          Comment

          • DU

            #6
            Re: Referencing windows by name?

            Robert Mark Bram wrote:
            [color=blue]
            > Hi Thomas.
            >
            > Your advice is pretty much what I originally tried. Here is a test scenario
            > for that. My problem is that on each alert, the 'placed' window property is
            > undefined!
            >
            > I am getting really frustrated here. Any advice would be most welcome!
            >
            > Rob
            > :)
            >[/color]

            Your code has several problems, some are logical ones.
            [color=blue]
            > ============
            > menu.htm
            >
            > ============
            > <html><head>
            > <script language="JavaS cript" type="text/javascript"
            > src="windowCode .js"></script>[/color]

            If you want your code to work as best as possible in all compliant
            browsers, then use a doctype declaration (preferably with a full url -
            system id -, strict DTD) and validate your files. Here, language is
            deprecated and invalid. type is both backward and forward-compatible, so
            you can't be wrong .
            [color=blue]
            > </head>[/color]


            <!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 and is mandatory</title>

            <script type="text/javascript" src="windowCode .js"></script>
            </head>

            <body>[color=blue]
            > <p>Menu:</p>
            > <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 language="JavaS cript" type="text/javascript"
            > src="windowCode .js"></script>
            > </head><body>
            > <p>Content 1</p>
            > <p><a href="menu.htm"
            > onClick="openMe nuWindow(this.h ref); return false;">Menu</a></p>
            > </body></html>
            >
            > ============
            > content2.htm
            >
            > ============
            > <html><head>
            > <script language="JavaS cript" type="text/javascript"
            > src="windowCode .js"></script>
            > </head><body>
            > <p>Content 2</p>
            > <p><a href="menu.htm"
            > onClick="openMe nuWindow(this.h ref); return false;">Menu</a></p>
            > </body></html>
            >
            >
            > ============
            > windowCode.js
            >
            > ============
            > // Menu window uses this function to open a content window.
            > function openContentWind ow (pageUrl)
            > {
            > var win = null;[/color]

            Your window object reference (win) is a local variable, not a global
            variable. This has decisive importance in the way your script functions
            and window object references can work interactively and efficiently.
            [color=blue]
            >
            >
            > alert ("window.conten tWindow = " + window.contentW indow);
            >[/color]

            Your original post had contentWindow defined as a global variable and a
            window object reference to a secondary window. Here, in this post, you
            don't define it that way. So, it's already impossible to test, verify
            your code. Best is to upload all these files in a site and then give an
            url so that we can test, even debug the webpage application.
            One other thing. If contentWindow is a window object reference, then
            window.contentW indow is circular and unneedlessly redundant, auto-circular.
            [color=blue]
            > if (window.content Window &&
            > !window.content Window.closed)
            > {
            > win = window.contentW indow;[/color]

            I would drop entirely resorting to this local variable (win) to store a
            global reference: it's counter-intuitive and not needed.
            [color=blue]
            > if (String(win.loc ation) != pageUrl)
            > {
            > win.location.re place (pageUrl);
            > } // end if
            > } // end if
            > else
            > {
            > win = window.open (pageUrl, "contentWindow" );[/color]

            Bad coding practice. You should always give distinct, unique, intuitive
            and meaningful identifiers. Here, the window.name and the window object
            reference identifier is the same. So this makes debugging more difficult
            for reviewers or yourself later. Even when using application like a
            javascript debugger, this makes the untangling of code unneedlessly more
            difficult.
            [color=blue]
            > window.contentW indow = win;[/color]

            This looks tortuous at best.
            [color=blue]
            > } // end else
            > win.menuWindow = self;
            > win.focus();[/color]

            focus() calls should be done only if the targeted window has effectively
            lost focus, otherwise this is a poor usage of user's system resources.
            [color=blue]
            > } // end openContentWind ow function
            >
            >
            >
            >
            > // Content windows use this function to open a menu window.
            > function openMenuWindow (pageUrl)//(pageUrl, pageName, w, h, scroll, pos)
            > {
            > window.name="co ntentWindow";
            >
            > var win = null;
            >
            > alert ("self.menuWind ow = " + self.menuWindow );
            >
            > if (window.menuWin dow &&
            > !window.menuWin dow.closed)
            > {
            > win = window.menuWind ow;
            > if (String(win.men uWindow) != pageUrl)
            > {
            > win.location.re place (pageUrl);
            > } // end if
            > } // end if
            > else
            > {
            > win = newMenuWindow (pageUrl);
            > window.menuWind ow = win;[/color]

            The previous 2 lines up there are illogical, at best, redundant.
            Compacted, it ends up as:

            menuWindow = newMenuWindow(p ageUrl);
            where menuWindow is a local variable in your newMenuWindow function and
            on the left side is a global variable. It's really confusing.

            [color=blue]
            > } // end else
            >
            > win.contentWind ow = self;
            > win.focus();
            > } // end openMenuWindow function
            >
            >
            > function newMenuWindow (pageUrl)
            > {
            > var pageName = 'menuWindow';
            > var w = '800';
            > var h = '450';
            > var scroll = 'no';
            > var pos = 'center';[/color]

            pos is never used in your code.
            [color=blue]
            >
            > var LeftPosition=(s creen.width)?(s creen.width-w)/2:100;
            > var TopPosition=(sc reen.height)?(s creen.height-h)/2:100;
            >[/color]

            Your code ignores space taken by os-dependent semi-permanent
            applications. Explorer bar, NS/Mozilla sidebar, MS-magnify, MS-Office
            quick launch bar, winbar, windows taskbar, etc.. are just examples of
            applications re-defining the workarea for applications where a new
            window can be positioned. The correct way to center a new window is to
            consider the available screen dimensions for applications (referred as
            workspace or workarea for applications in documentations) .
            [color=blue]
            > var settings=
            > 'width='+w+
            > ',height='+h+
            > ',top='+TopPosi tion+
            > ',left='+LeftPo sition+
            > ',scrollbars='+ scroll+
            >
            > ',location=no,d irectories=no,s tatus=no,menuba r=no,toolbar=no ,resizable=no';
            >[/color]

            You're willing to turn off window resizability for a window which will
            have a minimum of 800x450 dimensions. This can not promote usability.
            You're also willing to turn off scrollbars if needed, that is if content
            overflows your own requested window dimensions: this is
            counter-usability and anti-userfriendly. Your code does not take into
            consideration default font-size on the user's browser and studies show
            that this is an important usability factor when browsing.

            You're also willing to turn off status bar on a 800x450 window. Status
            bar reports unbiased reliable notifications on connections, download,
            SSL icon, security warnings, download progress, etc.. Why would you need
            to remove the status bar?
            [color=blue]
            > var menuWindow = window.open (pageUrl, pageName, settings);
            > return menuWindow;
            > } // end openContentWind ow function
            >
            >[/color]

            var menuWindowObjec tReference;
            function newMenuWindow (pageUrl)
            {
            var windowWidth, windowHeight, windowLeft, windowTop;

            if(typeof window.screenX == "number" && typeof window.innerWid th ==
            "number")
            {
            windowWidth = window.innerWid th * .68;
            windowHeight = window.innerHei ght * .68;
            windowLeft = window.screenX + window.innerWid th * .16;
            windowTop = window.screenY + window.innerHei ght * .16;
            }
            else if(typeof screen.availWid th == "number")
            {
            windowWidth = screen.availWid th * .68;
            windowHeight = screen.availHei ght * .5;
            windowLeft = screen.availWid th * .16;
            windowTop = screen.availHei ght * .16;
            }
            else
            {
            windowWidth = 500;
            windowHeight = 250;
            windowLeft = 60;
            windowTop = 40;
            };


            menuWindowObjec tReference = window.open(pag eUrl, "menuWindowName ",
            "left=" + windowLeft + ",top=" + windowTop + ",width=" + windowWidth +
            ",height=" + windowHeight + ",resizable,scr ollbars,status" );
            }

            I'm sure your problem is solvable. Can you upload all your files into a
            website? I would look at it.

            DU
            --
            Javascript and Browser bugs:

            - Resources, help and tips for Netscape 7.x users and Composer
            - Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x




            Comment

            • Robert Mark Bram

              #7
              Re: Referencing windows by name?

              Hi DU,

              My testing pages can now be found here:





              All style, compatibility and naming issues aside (which I take to heart and
              thank you for pointing out), there is one point I would like to ask you
              about here.
              [color=blue]
              > Your window object reference (win) is a local variable, not a global
              > variable. This has decisive importance in the way your script functions
              > and window object references can work interactively and efficiently.[/color]

              A friend suggested this to me before - but I fail to see how this can be the
              cause of my problems since in all cases win is assigned:
              - a property from the current window object; or
              - the value of a new window (which is then stored as a property in the
              current window object).
              Either way, while win maybe local, it is a reference to a window object that
              is not local (afaik) - how can DOM's window for the currently executing page
              be local?

              On a point of style, I believe it to be a good thing to use a local variable
              here. I would think that the following code:

              win = window.contentW indow;
              if (String(win.loc ation) != pageUrl)
              {
              win.location.re place (pageUrl);

              is far more readable than this code:

              win = window.contentW indow;
              if (String(window. contentWindow.l ocation) != pageUrl)
              {
              window.contentW indow.location. replace (pageUrl);

              Although win could be re-named as "contentWin " in openContentWind ow() and
              "menuWin" in openMenuWindow( ).

              Thank you very much for your response and the time taken to prepare it.

              Rob
              :)


              Comment

              • Robert Mark Bram

                #8
                Re: Referencing windows by name?

                Hi DU,

                One other question..
                [color=blue][color=green]
                > > } // end else
                > > win.menuWindow = self;
                > > win.focus();[/color]
                >
                > focus() calls should be done only if the targeted window has effectively
                > lost focus, otherwise this is a poor usage of user's system resources.[/color]

                If the window already exists with the correct page loaded, I wish to bring
                the window to the top (and not reload anything).. is there another way apart
                from focus() to do this?

                Rob
                :)


                Comment

                • DU

                  #9
                  Re: Referencing windows by name?

                  Robert Mark Bram wrote:
                  [color=blue]
                  > Hi DU,
                  >
                  > One other question..
                  >
                  >[color=green][color=darkred]
                  >>> } // end else
                  >>> win.menuWindow = self;
                  >>> win.focus();[/color]
                  >>
                  >>focus() calls should be done only if the targeted window has effectively
                  >>lost focus, otherwise this is a poor usage of user's system resources.[/color]
                  >
                  >
                  > If the window already exists with the correct page loaded,[/color]

                  then right here you first need to test if the window object reference
                  exists in memory and then if the referenced window is not closed.

                  I wish to bring[color=blue]
                  > the window to the top (and not reload anything)..[/color]

                  But your code does none of this: it just calls win.focus() even if it is
                  not justified to do so. The standard way to do things is to verify
                  existence of an object and support for a method and then call the method
                  if the context justifies doing so.
                  What I'm trying to say is
                  bring it back on top if it lost focus, if it is behind the opener and do
                  nothing if it has not lost focus.
                  Your code says: bring it on top whatever. That's not sound programming.

                  Your code uses so many window references assignments, so many local
                  variable storing window references, etc.. It's really spaghetti-like.
                  Sorry if I sound admonishing or scolding you (I'm not trying to be hard
                  on you)... but that is what I see in your posts.

                  --
                  DU


                  Comment

                  • Robert Mark Bram

                    #10
                    Re: Referencing windows by name?

                    Hi DU!
                    [color=blue]
                    > I wish to bring[color=green]
                    > > the window to the top (and not reload anything)..[/color]
                    >
                    > But your code does none of this: it just calls win.focus() even if it is
                    > not justified to do so. The standard way to do things is to verify
                    > existence of an object and support for a method and then call the method
                    > if the context justifies doing so.[/color]

                    That is what the code does.

                    if (window.content Window &&
                    !window.content Window.closed)
                    {
                    win = window.contentW indow;
                    if (String(win.loc ation) != pageUrl)
                    {
                    win.location.re place (pageUrl);
                    } // end if
                    } // end if
                    else
                    {
                    win = window.open (pageUrl, "contentWindow" );
                    window.contentW indow = win;
                    } // end else

                    This will assign a value to 'win' - if contentWindow is there then will be
                    assigned
                    window.contentW indow
                    other wise it is assigned
                    window.open

                    I do not believe focus is being called unnecisarily -- focus() is only
                    called as part of a call to openContentWind ow() (from the menu window) or
                    openMenuWindow( ) (from the content window). In either case, it is one window
                    bringing the *other* into focus.

                    [color=blue]
                    > Your code uses so many window references assignments, so many local
                    > variable storing window references, etc.. It's really spaghetti-like.
                    > Sorry if I sound admonishing or scolding you (I'm not trying to be hard
                    > on you)... but that is what I see in your posts.[/color]

                    What I want to do is manipulate two window objects - contentWindow and
                    menuWindow - and allow each to bring the other back into focus without
                    reloading the document where possible.

                    Rob
                    :)


                    Comment

                    • DU

                      #11
                      Re: Referencing windows by name?

                      Robert Mark Bram wrote:
                      [color=blue]
                      > Hi DU,
                      >
                      > My testing pages can now be found here:
                      > http://members.optushome.com.au/suesayram/rob/menu.htm
                      > http://members.optushome.com.au/sues...b/content1.htm
                      > http://members.optushome.com.au/sues...b/content2.htm
                      > http://members.optushome.com.au/sues.../windowCode.js
                      >[/color]

                      Your original post had contentWindow defined as a global variable and a
                      window object reference to a secondary window. Here, in your previous
                      post and still in your uploaded files, you don't define it that way. So
                      what should I be debugging here? What is your real code?
                      You did not include in these files any of the recommendations I suggested.
                      [color=blue]
                      > All style, compatibility and naming issues aside (which I take to heart and
                      > thank you for pointing out), there is one point I would like to ask you
                      > about here.
                      >[/color]

                      You can not ignore the usability issues here and what a vast experience
                      of coding and doing popups has gathered over the years: these are very
                      important for the success of your webpages. Here's food for you to think
                      about:

                      "Research shows that most users don't like to run more than one
                      application at a time. In fact, many users are confused by multiple
                      applications."

                      Windows User Experience team,
                      Microsoft Windows User Experience Frequently Asked Questions: Why is the
                      taskbar at the bottom of the screen?,
                      March 2001

                      "(...) Users often don't notice that a new window has opened, especially
                      if they are using a small monitor where the windows are maximized to
                      fill up the screen. So a user who tries to return to the origin will be
                      confused by a grayed out Back button." Jakob Nielsen, The Top Ten New
                      Mistakes of Web Design: 2. Opening New Browser Windows, May 30, 1999

                      and we know that you impose 2 distinct 800x450 secondary windows here.
                      So the user has to be able to juggle confortably with 3 opened windows
                      (menu.htm, content1.htm, content2.htm). You're definitively not making
                      this easy for a majority of users.

                      "(...) spawning second browser windows can completely throw users off
                      track because it removes the one thing they are sure how to use: the
                      'Back' button.(...) In another recent study, six out of 17 users had
                      difficulty with multiple windows, and three of them required assistance
                      to get back to the first window and continue the task."
                      Carolyn Snyder, Seven tricks that Web users don't know: 7. Second
                      browser windows, June 2001
                      [color=blue]
                      >[color=green]
                      >>Your window object reference (win) is a local variable, not a global
                      >>variable. This has decisive importance in the way your script functions
                      >>and window object references can work interactively and efficiently.[/color]
                      >
                      >
                      > A friend suggested this to me before - but I fail to see how this can be the
                      > cause of my problems since in all cases win is assigned:[/color]

                      Maybe and then maybe not. What are your chances of self-creating a
                      logical bug by making your code more intricatedly complex? Have you an
                      idea of the side effects of toying with multiple local variables
                      interacting with global variables? What's the safe, sane, sound and most
                      efficient way to code here?

                      I did not see obvious misses with your code... but I know from
                      experience that you should keep number of variables to a minimum and not
                      use variables which duplicate unneedlessly the work of other variables.
                      It's not only a debugging issue, a code maintenance issue, a sound code
                      practice issue: it's a memory calls, stack, memory management issue on
                      the user's system. Crashes sometimes happens because of undetected
                      memory management conflicts.
                      [color=blue]
                      > - a property from the current window object; or[/color]

                      which is a global variable storing a reference to a window object. Why
                      play with 2 classes of memory... when you do not absolutely need to?
                      [color=blue]
                      > - the value of a new window (which is then stored as a property in the
                      > current window object).
                      > Either way, while win maybe local, it is a reference to a window object that
                      > is not local (afaik)[/color]

                      Correct. So, just answer me: is that sound, logical and suitable in your
                      opinion?

                      - how can DOM's window for the currently executing page[color=blue]
                      > be local?
                      >
                      > On a point of style, I believe it to be a good thing to use a local variable
                      > here.[/color]

                      Well, ask around in this newsgroup then. Trust me: you'll get a
                      different feedback.

                      I would think that the following code:[color=blue]
                      >
                      > win = window.contentW indow;
                      > if (String(win.loc ation) != pageUrl)
                      > {
                      > win.location.re place (pageUrl);
                      >
                      > is far more readable than this code:
                      >
                      > win = window.contentW indow;
                      > if (String(window. contentWindow.l ocation) != pageUrl)
                      > {
                      > window.contentW indow.location. replace (pageUrl);[/color]

                      var contentWindowOb jectReference ; // global variable
                      function openContentWind ow(pageUrl)
                      {
                      if(contentWindo wObjectReferenc e != null &&
                      !contentWindowO bjectReference. closed)
                      {
                      if(contentWindo wObjectReferenc e.location.href != pageUrl)
                      {
                      contentWindowOb jectReference.l ocation.replace (pageUrl);
                      };
                      }
                      else
                      {
                      contentWindowOb jectReference = window.open(pag eUrl,
                      "contentWindowN ame", "...windowFeatu res string list...");
                      };
                      }

                      I even think at this point that your function name identifier is also
                      confusing: you're replacing content or you're creating and opening that
                      content window really. You should not assume that people reviewing your
                      code will easily figure out what you mean to do when you do not give
                      best meaningful identifiers to your function name, variables, etc..[color=blue]
                      >
                      > Although win could be re-named as "contentWin " in openContentWind ow() and
                      > "menuWin" in openMenuWindow( ).
                      >[/color]

                      Although win could be entirely removed everywhere in these functions in
                      a sane effort of simplifying the code, making it more
                      memory-management-efficient, straightforward , easier to debug, etc...:
                      yes, that is what I'm saying.
                      You don't even need to prefix window object references with window either.

                      Just get rid entirely and everywhere of all these local win variables
                      and just work on global variables which store references to window
                      objects. Simplify your code!

                      DU

                      P.S.: just got an idea of what I think you're trying to achieve here.
                      Can you have a look at this page and then tell me if that is what you're
                      trying to achieve here?



                      .... because in that page, I recycle and reuse the same requested popup:
                      I just change the url of the popup.
                      [color=blue]
                      > Thank you very much for your response and the time taken to prepare it.
                      >
                      > Rob
                      > :)
                      >
                      >[/color]


                      Comment

                      • DU

                        #12
                        Re: Referencing windows by name?

                        Robert Mark Bram wrote:
                        [color=blue]
                        > Hi DU!
                        >
                        >[color=green]
                        >> I wish to bring
                        >>[color=darkred]
                        >>>the window to the top (and not reload anything)..[/color]
                        >>
                        >>But your code does none of this: it just calls win.focus() even if it is
                        >>not justified to do so. The standard way to do things is to verify
                        >>existence of an object and support for a method and then call the method
                        >>if the context justifies doing so.[/color]
                        >
                        >
                        > That is what the code does.[/color]

                        No. Your function calls the focus method regardless of context.
                        [color=blue]
                        >
                        > if (window.content Window &&
                        > !window.content Window.closed)
                        > {
                        > win = window.contentW indow;
                        > if (String(win.loc ation) != pageUrl)
                        > {
                        > win.location.re place (pageUrl);
                        > } // end if
                        > } // end if
                        > else
                        > {
                        > win = window.open (pageUrl, "contentWindow" );
                        > window.contentW indow = win;
                        > } // end else
                        >
                        > This will assign a value to 'win' - if contentWindow is there then will be
                        > assigned
                        > window.contentW indow
                        > other wise it is assigned
                        > window.open
                        >
                        > I do not believe focus is being called unnecisarily -- focus() is only
                        > called as part of a call to openContentWind ow() (from the menu window) or
                        > openMenuWindow( ) (from the content window).[/color]

                        Your focus call is called every single time that openContentWind ow is
                        called. Every time.

                        In either case, it is one window[color=blue]
                        > bringing the *other* into focus.
                        >
                        >
                        >[color=green]
                        >>Your code uses so many window references assignments, so many local
                        >>variable storing window references, etc.. It's really spaghetti-like.
                        >>Sorry if I sound admonishing or scolding you (I'm not trying to be hard
                        >>on you)... but that is what I see in your posts.[/color]
                        >
                        >
                        > What I want to do is manipulate two window objects - contentWindow and
                        > menuWindow -[/color]

                        Your code could be made more compact, more straightforward , more
                        efficient. I am again repeating myself.

                        and allow each to bring the other back into focus without[color=blue]
                        > reloading the document where possible.
                        >
                        > Rob
                        > :)
                        >
                        >[/color]


                        Just answer me then. Did you post or not the following? Just yes or no.
                        Fair enough?

                        function openContentWind ow (pageUrl)
                        {
                        var win = null;


                        alert ("window.conten tWindow = " + window.contentW indow);

                        if (window.content Window &&
                        !window.content Window.closed)
                        {
                        win = window.contentW indow;
                        if (String(win.loc ation) != pageUrl)
                        {
                        win.location.re place (pageUrl);
                        } // end if
                        } // end if
                        else
                        {
                        win = window.open (pageUrl, "contentWindow" );
                        window.contentW indow = win;
                        } // end else
                        win.menuWindow = self;
                        win.focus();
                        } // end openContentWind ow function

                        Where have I copied that code then?

                        DU

                        Comment

                        • Robert Mark Bram

                          #13
                          Re: Referencing windows by name?

                          Hi DU!

                          I get your point about focus now - I don't need to call it after
                          'window.open'. Thanks for that!
                          [color=blue][color=green]
                          > > My testing pages can now be found here:
                          > > http://members.optushome.com.au/suesayram/rob/menu.htm
                          > > http://members.optushome.com.au/sues...b/content1.htm
                          > > http://members.optushome.com.au/sues...b/content2.htm
                          > > http://members.optushome.com.au/sues.../windowCode.js[/color][/color]

                          I apologise if I confused the issue with my testing pages. Yes those pages
                          had different code from the code I posted originally. I have gone through
                          many revisions of the code in the past two days and the code I posted here
                          was my most recent attempt.

                          I also agree that working with multiple windows is generally a bad idea - I
                          was trying to work with only two windows. The actual application has a lot
                          more to it than the above testing pages. I am sorry if this puts you off
                          wanting to help me (I understand your reasons), but I am not asking for
                          design assistance; I just want to get the JavaScript correct. :(
                          [color=blue]
                          > P.S.: just got an idea of what I think you're trying to achieve here.
                          > Can you have a look at this page and then tell me if that is what you're
                          > trying to achieve here?
                          >
                          >[/color]
                          http://www10.brinkster.com/doctorunc...pera7Bugs.html[color=blue]
                          >
                          > ... because in that page, I recycle and reuse the same requested popup:
                          > I just change the url of the popup.[/color]

                          That's the effect! Except that I want a 'call back' - so the "pop-ups"
                          (which is my content window) have a link back to the menu that will focus on
                          the menu window if it is still open or reload it if it is not.

                          This is why I was trying to have two window objects referring back to each
                          other: with the idea that each window could simply 'focus' back on the other
                          when a link is hit..

                          Rob
                          :)


                          Comment

                          • Robert Mark Bram

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

                            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 ";
                            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');

                            // If it is a blank window then its location will be blank
                            // and should be reloaded.
                            if (String (menuWindow.loc ation) != pageUrl)
                            {
                            menuWindow.loca tion.replace (pageUrl);
                            } // end if
                            else
                            {
                            menuWindow.focu s();
                            } // end else
                            } // end openMenuWindow function


                            Comment

                            • DU

                              #15
                              Re: Referencing windows by name?

                              Robert Mark Bram wrote:[color=blue]
                              > Hi DU!
                              >
                              > I get your point about focus now - I don't need to call it after
                              > 'window.open'. Thanks for that!
                              >[/color]

                              It's sometimes difficult to understand each other in newsgroup postings.
                              [color=blue]
                              >[color=green][color=darkred]
                              >>>My testing pages can now be found here:
                              >>>http://members.optushome.com.au/suesayram/rob/menu.htm
                              >>>http://members.optushome.com.au/sues...b/content1.htm
                              >>>http://members.optushome.com.au/sues...b/content2.htm
                              >>>http://members.optushome.com.au/sues.../windowCode.js[/color][/color]
                              >
                              >
                              > I apologise if I confused the issue with my testing pages. Yes those pages
                              > had different code from the code I posted originally. I have gone through
                              > many revisions of the code in the past two days and the code I posted here
                              > was my most recent attempt.
                              >
                              > I also agree that working with multiple windows is generally a bad idea - I
                              > was trying to work with only two windows. The actual application has a lot
                              > more to it than the above testing pages. I am sorry if this puts you off
                              > wanting to help me (I understand your reasons), but I am not asking for
                              > design assistance; I just want to get the JavaScript correct. :(
                              >
                              >[color=green]
                              >>P.S.: just got an idea of what I think you're trying to achieve here.
                              >>Can you have a look at this page and then tell me if that is what you're
                              >>trying to achieve here?
                              >>
                              >>[/color]
                              >
                              > http://www10.brinkster.com/doctorunc...pera7Bugs.html
                              >[color=green]
                              >>... because in that page, I recycle and reuse the same requested popup:
                              >>I just change the url of the popup.[/color]
                              >
                              >
                              > That's the effect! Except that I want a 'call back' - so the "pop-ups"
                              > (which is my content window) have a link back to the menu that will focus on
                              > the menu window if it is still open or reload it if it is not.
                              >[/color]

                              Ok. So, you need to edit the content of that popup a bit, right? I first
                              need to see the changes before suggesting any modifications to your
                              code. I need to see the final latest version of your code.
                              [color=blue]
                              > This is why I was trying to have two window objects referring back to each
                              > other: with the idea that each window could simply 'focus' back on the other
                              > when a link is hit..
                              >
                              > Rob
                              > :)
                              >
                              >[/color]

                              DU

                              Comment

                              Working...