Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    209
    Thanks
    52
    Thanked 1 Time in 1 Post

    bringing open window to top

    I have some code that uses window.open to open a window in response
    to a mouse click event handler.

    If the user has used the button to open the window,
    then brings the window.opener window to the front,
    then clicks the button again,
    the window that is opened by code stays in the background.

    How do I get it to come to the front when the user clicks the button?

    Thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    [code]
    <script type="text/javascript">
    var theWindow = null; // make this a global variable

    ... somewhere else in the code ...
    theWindow = window.open("...the url...", "theWindowName" );


    .... anywhere else in the code ...
    if ( theWindow != null ) { theWindow.focus(); // bring it to front }

    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    anotherJEK (11-05-2012)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    209
    Thanks
    52
    Thanked 1 Time in 1 Post

    one more problem

    If I the user uses the browser window close button (or short cut)
    Javascript will not process an onclose event in that case. (using Firefox to develop,
    I have not tried this in other browsers, but I am guessing the same for them)
    So, the global variable representing the window does not reset to null.
    Then opening the window with the button will open a nonexistent window.

    That provokes another two part question:
    In spite of the paranoia about monitoring, it would be reasonable for a
    developer to determine if the user has used the window's close button
    or short cut to close a window, AND, it would be an enormous convenience
    to the whole net if the browser sent a query to the server when the last
    window displaying specific content was closed.
    (I also have done a lot of php and from the server side it would vastly
    simplify things like session handling and cleanup, even for non secure
    type tracking)
    Last edited by anotherJEK; 11-05-2012 at 08:14 PM. Reason: more relevant data

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Closing a web page (such as by closing the browser window or simpy swapping to a different page in that window will trigger an "unload" event. There is no such thing as a close event.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    It's not a full solution for your question/problem, but you could do this:
    Code:
    <script type="text/javascript">
    var theWindow = null; // make this a global variable
    
    ... somewhere else in the code ...
    theWindow = window.open("...the url...", "ZAMBONI" );
    theWindow.onunload = function( ) { theWindow = null; } // see below
    
    .... anywhere else in the code ...
    if ( theWindow != null && theWindow.name == "ZAMBONI" ) { theWindow.focus(); // bring it to front }
    
    </script>
    If you only want to set theWindow to null if the user navigates to another domain or completely closes the window, you could pull a few hacky tricks.

    For example, when the window unloads, just do a setTimeout() and, after a second or two, have your code go see if it can access the window contents. If it can, then the window is still in your domain. If not, it's either closed or in another domain.

    Just throwing ideas in the wind here. Play around. You can likely find a way to do most if not all of what you are trying to do.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    anotherJEK (11-05-2012)

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    209
    Thanks
    52
    Thanked 1 Time in 1 Post

    onunload event

    I know there is no close event.
    What leads me to believe the using the browsers chrome close button,
    or window closing short cut will not be processed by javascript is that I
    have been experimenting with code that runs when the unload event handler
    is supposed to be called. For instance having an alert called when the window
    closes won't happen, or at least I could not get it to.
    I have an app the opens a window and allows user to edit string content.
    I have had to write a specific warning not to use the chrome window closing
    button or window closing short cut because it will dump all the changes that
    have been made. I want the user to be shown a warning confirmation dialog
    to allow them to save to the opening window or the server. This is what happens
    in applications that run locally, but not browser based applications running javascript
    or ActionScript because they are not allowed local file system access.
    What would happen if I closed and used history to navigate back to the browser window
    when I am composing this reply?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Have you looked into window.onbeforeunload?

    What it can do is limited, but it's still pretty useful for stuff like this.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    209
    Thanks
    52
    Thanked 1 Time in 1 Post

    window.onbeforeunload

    This is an IE specific event isn't it?

    anyhow here is what appears to work for open and closed windows,
    assuming the window.opener window is still opened:
    Code:
       this.helpWindow = null;
       this.show_help = function()
                               {
                                if(this.helpWindow == null || !this.helpWindow.name )
                                  {
                                   this.helpWindow = open('SC_docs/calc_manual.html', 'Calculator_2_manual', 'height=600,width=700,scrollbars=yes,resizeable=yes');
                                   this.helpWindow.document.close();
                                  }
                                else
                                  {
                                   this.helpWindow.focus();
                                  }
                               }
    <window name>.document.close() is for the sake of Firefox which will
    never finish loading the window without it (or some versions of Firefox.)


  •  

    Posting Permissions

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