SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get focus to a window that you are not a parent of

    My scenario is this: I have 2 totally separate web applications running on the same app server. When app 1 is open, and app 2 isn't, there is a link (on the app 1 page) to open app 2. App 2 opens no problem. I am now the parent window of app 2 and if I hit the link to open the app 2 again, I have a reference to this child window and can either A) reopen app 2 if it has been closed, or B) just put focus to app 2 since it is already open. Everything is good so far.

    Here is the problem:
    New Scenario -
    Let's say I open app 2 separately. I then open app 1. App 1 has the link to open app 2. I no longer have a reference to app 2 window, so I can't check if it is already open. If I have been navigating around in app 2 and making some changes, the link I hit in app 1 to open app 2 DOES bring focus to the app 2 window, but it brings me back to the home page of app 2...so it's like it knows about the window name and replaces the appropriate window with the app, but I don't want to overwrite it if it is already open, jsut gain focus. Is there a way to check if a window is open that isn't a child window? It somehow knows enough of the window name to replace and bring focus to this window with app 2, but I want to be able to check if it is already open in this scenario.


    This was probably hard to follow but I explained it the best I could without pictures. Thanks in advance.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens if you set the first parameter of the call to window.open() to an empty string?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem there is if app 2 isn't open and I pass an empty string, it doesn't open app 2.

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have been more specific.

    What happens if you set the first parameter of the call to window.open() to an empty string when app 2 is already open?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good call. it does correctly bring focus to the app 2 window if app 2 is already open. obviously, the next question is how to bring up app 2 if it's not currently open.

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:
    Code:
    var newWin = window.open('','someWindowName');
    if(newWin.location.href != 'theURLforApp2') {
      newWin.location.href = 'theURLforApp2';
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. This does work, with the exception of that I can't call focus() on the window. I get an error (I forgot exactly what it was) but it was something to the effect that I can't call this method and after some research it looked like it had to do with the apps running on different domains? I don't suppose there is any way around this one. I'm going to look into it more on Monday.

  8. #8
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kravvitz,
    Thanks for your insight into this.

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I decided to take one last look into this and came upon something. It appears that there are a few functions you can call that will bypass the security restrictions and focus() is one of them. I found the article here:
    http://www.devarticles.com/c/a/JavaS...pt-Security/1/

    I will post the code here in case it will possibly help someone in the future:

    Code:
     var newWin = null;
     function openApp2Window(windowURL, width) {
          newWin = window.open('','app2Window');
          newWin.focus();
        if(newWin.location.href != windowURL) {
              newWin.location.href = windowURL;
        }
     }
    The problem that I originally noted with the focus() function was not in the focus() function itself, but with the fact that I was trying to call focus() AFTER the line that says "if(newWin.location.href != windowURL) {"
    If the window is not already open, that line executed fine, but if the app2 window was open already it died on that line and therefore never made it to the focus() call. Once I moved the focus call above that line everything works great now.

    One thing to note is that I have not tried this across browsers. This is an internal app at work and users are restricted to using IE6.


Bookmarks

Posting Permissions

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